Last active
March 5, 2018 16:08
-
-
Save davidejones/6113695a8a65202f04bb21c9fdb63212 to your computer and use it in GitHub Desktop.
eloquentjavascript : chapter 3
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
// minimum | |
function min(arg1, arg2) { | |
return (arg1 < arg2) ? arg1 : arg2; | |
} | |
console.log(min(0, 10)); | |
console.log(min(0, -10)); | |
// recursion | |
function isEven(n) { | |
if(n < 0) throw "Must be a whole positive number"; | |
if(n <= 1) return (n === 0) ? true : false; | |
return isEven(n-2); | |
} | |
console.log(isEven(50)); | |
console.log(isEven(75)); | |
console.log(isEven(-1)); | |
// bean counting | |
function countBs(s) { | |
var out = 0; | |
for(var i=0; i < s.length; i++) { | |
if(s[i] === 'B') out++; | |
} | |
return out; | |
} | |
function countChar(s, char) { | |
var out = 0; | |
for(var i=0; i < s.length; i++) { | |
if(s[i] === char) out++; | |
} | |
return out; | |
} | |
console.log(countBs("BBC")); | |
console.log(countChar("kakkerlak", "k")); |
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
// minimum | |
0 | |
-10 | |
// recursion | |
true | |
false | |
Must be a whole positive number | |
// bean counting | |
2 | |
4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment