Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions solutions/128.js
Original file line number Diff line number Diff line change
@@ -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,
};
25 changes: 25 additions & 0 deletions test/128.js
Original file line number Diff line number Diff line change
@@ -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);
});
});