diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-03-08 12:51:49 +0100 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-03-08 12:51:49 +0100 |
| commit | 4e9c0237d9b4a46dff0c8e80316ad2f4886fae7d (patch) | |
| tree | 30381a22450f99bf95ef1dc137fd152662e924a5 /index.js | |
| parent | 0729c707f61254fbff440cf52daff23ae08e148f (diff) | |
write first test
Diffstat (limited to 'index.js')
| -rw-r--r-- | index.js | 11 |
1 files changed, 8 insertions, 3 deletions
@@ -19,7 +19,7 @@ basic.validate_digits = function(s, b) { // Validate that a base is valid. basic.validate_base = function(b){ - return (b === 0 || b === 1 || b === -1 && (Math.abs(b) % 2) === 0) + return (b !== 0 && b !== 1 && b !== -1 && (Math.abs(b) % 2) === 0) } // Convert a string s representing a number in base b @@ -45,7 +45,7 @@ basic.toString = function (n, b) { var s = "" var dividend, residual while (n) { - remainder = mod(n, b) + remainder = basic.mod(n, b) n = Math.floor(n/b) if (remainder) { n += 1 @@ -55,5 +55,10 @@ basic.toString = function (n, b) { return s } -function mod(n,m){ return n-(m * Math.floor(n/m)) } +// Compute the residual of N mod M +// Handle negative N and M correctly +basic.mod = function (n,m){ + m = Math.abs(m) + return n-(m * Math.floor(n/m)) +} |
