summaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-03-08 12:51:49 +0100
committerJules Laplace <jules@okfoc.us>2017-03-08 12:51:49 +0100
commit4e9c0237d9b4a46dff0c8e80316ad2f4886fae7d (patch)
tree30381a22450f99bf95ef1dc137fd152662e924a5 /index.js
parent0729c707f61254fbff440cf52daff23ae08e148f (diff)
write first test
Diffstat (limited to 'index.js')
-rw-r--r--index.js11
1 files changed, 8 insertions, 3 deletions
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))
+}