From 1aaa8656b1b45fe69e7bf4cdfebc6360f21c978e Mon Sep 17 00:00:00 2001 From: caoglish Date: Fri, 23 Jan 2015 09:54:55 +1100 Subject: [PATCH 1/2] improve the byte-format.js: using math instead of loop. --- solutions/javascript/byte-format.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/solutions/javascript/byte-format.js b/solutions/javascript/byte-format.js index 86df2d0..5d3bf46 100644 --- a/solutions/javascript/byte-format.js +++ b/solutions/javascript/byte-format.js @@ -1,16 +1,9 @@ module.exports = function (value, precision) { var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; var factor = Math.pow(10, precision > 0 ? precision : 2); - var suffix = 0; - - while (value >= 1024 && suffixes[++suffix] && suffix < suffixes.length) { - value /= 1024; - } - - if (suffix >= suffixes.length) { - suffix = suffixes.length - 1; - } - - // Return the number rounded to precision. - return (Math.round(value * factor) / factor) + ' ' + suffixes[suffix]; + var suffix=Math.min (Math.floor(Math.log(value)/Math.log(1024)), + suffixes.length-1); + return (Math.round(value/Math.pow(1024,suffix) * factor) / factor) + + ' ' + + suffixes[suffix]; }; \ No newline at end of file From 629f3f86542fda32e1473906d1243738f744c9ac Mon Sep 17 00:00:00 2001 From: caoglish Date: Mon, 9 Jan 2017 14:59:39 +1100 Subject: [PATCH 2/2] - add phpunit test suites --- .gitignore | 1 + composer.json | 5 +++++ phpunit.xml | 6 ++++++ tests/php/factorialTest.php | 17 +++++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 tests/php/factorialTest.php diff --git a/.gitignore b/.gitignore index a59ae71..a8dec1e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea node_modules *.o +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..65658fb --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "phpunit/phpunit": "5.5.*" + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..ae3c20e --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,6 @@ + + + + tests/php + + \ No newline at end of file diff --git a/tests/php/factorialTest.php b/tests/php/factorialTest.php new file mode 100644 index 0000000..437a2f4 --- /dev/null +++ b/tests/php/factorialTest.php @@ -0,0 +1,17 @@ +assertEquals(1,factorial(1) ); + $this->assertEquals(2,factorial(2) ); + $this->assertEquals(6,factorial(3) ); + $this->assertEquals(120,factorial(5) ); + $this->assertEquals(2432902008176640000,factorial(20) ); + } +} +?> \ No newline at end of file