From 4e9c0237d9b4a46dff0c8e80316ad2f4886fae7d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 8 Mar 2017 12:51:49 +0100 Subject: write first test --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'index.js') diff --git a/index.js b/index.js index f168d16..6218073 100644 --- a/index.js +++ b/index.js @@ -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)) +} -- cgit v1.2.3-70-g09d2