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; |
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
// Calculate and show the value of 2**10 | |
var total = 1; | |
var i = 0; | |
var power = function (n) { | |
while (i < 10) { | |
total *= 2; | |
i++; | |
}; | |
return total; |
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
//Print out ten lines. On the first line there is one '#' character. On the second there are two. And so on. | |
var string = "#"; | |
var i = 0; | |
var line ="" | |
while (10 > i) { | |
line += string | |
document.write(line+"\n"); | |
i++; | |
} |
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
//Write a program to ask yourself, using prompt, what the value of 2 + 2 is. If the answer is "4", use alert to say something praising. If it is "3" or "5", say "Almost!". In other cases, say something mean. | |
answer = Number(prompt("What is the value of 2+2?")); | |
if (answer === 4) { | |
alert("you are right!"); | |
} else if (Math.abs(4 - answer) > 2) { | |
alert("you did okay"); | |
} else { | |
alert("lets go back to kindergarten"); | |
} |
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 greaterThan(num) { | |
return function test(num1) { | |
if (num > num1) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
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
var arr = []; | |
function list(n) { | |
for (var i = 0; i <= n; i++) { | |
arr[i] = i; | |
} | |
document.write(arr); | |
} |
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 startsWith(a, b) { | |
if (b.slice(0, a.length) == a) return true; | |
else return false; | |
} | |
a="steve" | |
b="steve is coding hard" | |
document.write(startsWith(a,b)); |
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
var mailArchive = retrieveMails(); | |
var livingCats = {"Spot": true}; | |
for (var mail = 0; mail < mailArchive.length; mail++) | |
var paragraphs = mailArchive[mail].split("\n"); //split the paragraphs | |
for (var paragraph = 0; paragraph < paragraph.length; paragraph++){ | |
if (startsWith(paragraph, "born")){ | |
var names = catNames(paragraphs[paragraph]) | |
for (var name = 0; name < names.length; names++) | |
livingCats[names[name]] = true; |
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 range(start, end, step) { | |
var arr = []; | |
if (arguments[2] == undefined) { | |
for (var i = start; i <= end; i++) | |
arr.push(i); | |
} else { | |
for (var i = start; i <= end; i += step) | |
arr.push(i); | |
} | |
return arr; |
OlderNewer