forked from llipio/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//Return the sum of all factors in x | ||
//Intialize sum to equal 0 | ||
//Factor i of number have mod equal to 0 | ||
//Reassign sum to equal to sum plus i | ||
//Run through for loop | ||
//input [2,4,10,24] | ||
//output [3,7,18,60] | ||
|
||
// Solution by Colin Xie @ColinX13 | ||
|
||
const solution = (x) => { | ||
let sum = 0 | ||
for(let i = x; i >= 1; i--){ | ||
if(x % i === 0){ | ||
sum = sum + i | ||
}; | ||
}; | ||
return sum | ||
}; | ||
module.exports = { | ||
solution | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const expect = require('chai').expect; | ||
let solution = require('../solutions/60').solution; | ||
// solution = require('./yourSolution').solution; | ||
|
||
describe('sum of factors', () => { | ||
it('sum of the factors of 2 is 3', () => { | ||
expect(solution(2)).eql(3); | ||
}); | ||
it('sum of the factors of 4 is 7', () => { | ||
expect(solution(4)).eql(7); | ||
}); | ||
it('sum of the factors of 10 is 18', () => { | ||
expect(solution(10)).eql(18); | ||
}); | ||
it('sum of the factors of 24', () => { | ||
expect(solution(24)).eql(60); | ||
}); | ||
}); | ||
|