Created
January 2, 2018 14:34
-
-
Save isabolic/a880fcc835965606ee8f8603e400aaf4 to your computer and use it in GitHub Desktop.
Multiplesof3or5
This file contains hidden or 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
| function solution(maxN){ | |
| let numbers=[3,5]; | |
| let passingNs = []; | |
| let ret = 0; | |
| for (let i = 1; i < maxN; i++) { | |
| numbers.forEach(n => { | |
| if (i * n < maxN && passingNs.indexOf((i*n)) === -1) { | |
| passingNs.push(i*n); | |
| } | |
| }); | |
| } | |
| passingNs.forEach(n => { | |
| ret = ret + n; | |
| }); | |
| return ret; | |
| } | |
| // tests | |
| function test(n, expected) { | |
| let actual = solution(n) | |
| Test.assertEquals(actual, expected, `Expected ${expected}, got ${actual}`) | |
| } | |
| Test.describe("basic tests", function(){ | |
| test(10,23) | |
| test(20,78) | |
| test(200,9168) | |
| }) | |
| Test.describe("smallest cases", function() { | |
| test(-1,0) | |
| test(0,0) | |
| test(1,0) | |
| test(2,0) | |
| test(3,0) | |
| test(4,3) | |
| test(5,3) | |
| test(6,8) | |
| }) | |
| function _solution(number){ | |
| var sum = 0; | |
| for(var i = 1; i< number; i++){ | |
| if(i % 3 == 0 || i % 5 == 0){ | |
| sum += i | |
| } | |
| } | |
| return sum; | |
| } | |
| Test.describe("random cases", function() { | |
| for(var i = 0; i < 10; i++) { | |
| let rand = Math.floor(Math.random() * 200) | |
| test(rand, _solution(rand)); | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. I was writing the code leaving out **function _solution(number){ };