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
Make the function abstract using reduce. | |
function countZeroes(numbers){ | |
function count(total,num){ | |
return total+(num ===0?1:0); <<===this is ternary equation!!! | |
} | |
return reduce(count, 0, numbers) | |
}; | |
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
//"died 27/04/2006: Black Leclère" | |
var p = "died 27/04/2006: Black Leclère" | |
function extractDate(p) { | |
function find(start, length) { | |
return p.slice(start, start + length) | |
} | |
return new Date(find(11, 4), find(8, 2) - 1, find(5, 2)) | |
} |
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; |
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 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 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 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
//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
//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
// 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; |