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
Do you know that the sort method does not work so well on integers in an array since it is used for strings! | |
That is something I found out today so then how to use a sort method on an array that has integers? Simply add a | |
custom function to a sort method as an argument like below! | |
var arr = [1,100,4,30,5,80] | |
var compare = function(num1,num2){ | |
if (num1 < num2){ | |
return -1; | |
} | |
else if (num1 > num2){ | |
return 1; |
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
/*If we list all the natural numbers below 10 that are multiples of 3 or 5, | |
we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
Find the sum of all the multiples of 3 or 5 below 1000. | |
*/ | |
var multiples = function (n) { | |
var sum = 0; | |
for (var i = 1; i < n; i++) { | |
if ((i % 3 == 0) || (i % 5 == 0)) { | |
sum += i; |
NewerOlder