Skip to content

Commit

Permalink
Implements multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
kleekich committed Sep 3, 2018
1 parent deb88a5 commit 62ac78c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,20 @@ var palindrome = function(string) {
// modulo(17,5) // 2
// modulo(22,6) // 4
var modulo = function(x, y) {
if(y=== 0) return NaN;
if(x < y) return x;
else if(x === y) return 0;
else if(x < 0) modulo(x+y, y);

else return modulo(x-y, y);
};

// 12. Write a function that multiplies two numbers without using the * operator or
// Math methods.
var multiply = function(x, y) {
if(x === 0 || y === 0 ) return 0;
else if (y < 0) return -x + multiply(x, y + 1);
else return x + multiply(x, y - 1);
};

// 13. Write a function that divides two numbers without using the / operator or
Expand Down

0 comments on commit 62ac78c

Please sign in to comment.