diff --git a/solutions/128.js b/solutions/128.js new file mode 100644 index 0000000..016a987 --- /dev/null +++ b/solutions/128.js @@ -0,0 +1,34 @@ +//Natalie Lockhart +//Multiply two numbers without using * + +const solution = (x, y) => { + function multiply(x,y){ + result += Math.abs(y); + counter++; + + if(counter == Math.abs(x)){ + return result; + } + multiply(x,y); + } + + if(x == 0 || y == 0){ + return 0; + } + + var counter = 0; + var result = 0; + multiply(x,y); + + //If one of the two numbers is negative, make the result negative + if ((Math.abs(x) != x || Math.abs(y) != y) && !(Math.abs(x) != x && Math.abs(y) != y)){ + result = 0 - result; + } + + return result; + +}; + +module.exports = { + solution, +}; diff --git a/test/128.js b/test/128.js new file mode 100644 index 0000000..bb7f782 --- /dev/null +++ b/test/128.js @@ -0,0 +1,25 @@ +const expect = require('chai').expect; +let solution = require('../solutions/128.js').solution; + +describe('Multiply two numbers without using *', () => { + it('should return 0 for 10,0', () => { + const result = solution(10,0); + expect(result).to.equal(0); + }); + it('should return 3 for 1,3', () => { + const result = solution(1,3); + expect(result).to.equal(3); + }); + it('should return 60 for 6,10', () => { + const result = solution(6,10); + expect(result).to.equal(60); + }); + it('should return -100 for 10,-10', () => { + const result = solution(10,-10); + expect(result).to.equal(-100); + }); + it('should return 25 for -5,-5', () => { + const result = solution(-5,-5); + expect(result).to.equal(25); + }); +});