Last active
July 20, 2017 23:52
-
-
Save abohannon/34d56e53cd1e26697a58a719482fb25e to your computer and use it in GitHub Desktop.
Eloquent Javascript Exercise Solutions
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
// My answers to the exercises in Eloquent Javascript | |
// Looping a triangle | |
var myStr = ''; | |
for (var i = 1; i < 7; i++){ | |
console.log(myStr += "#"); | |
} | |
// FizzBuzz | |
function fizzBuzz(num){ | |
for (var i = 1; i <= num; i++){ | |
if (i % 5 === 0 && i % 3 === 0){ | |
console.log("FizzBuzz"); | |
} else if (i % 5 === 0){ | |
console.log("Buzz"); | |
} else if (i % 3 === 0){ | |
console.log("Fizz"); | |
} else { | |
console.log(i); | |
} | |
} | |
} | |
fizzBuzz(100); | |
// Chess Board | |
function chessBoard(num){ | |
for (var i = 0; i < num; i++){ | |
var myStr = ''; | |
for (var k = 0; k < num; k++){ | |
var total = i + k; | |
if (total % 2){ | |
myStr += '#'; | |
} else { | |
myStr += ' '; | |
} | |
} | |
console.log(myStr); | |
} | |
} | |
chessBoard(8); | |
// Minimum | |
function min(numOne, numTwo) { | |
if (numOne < numTwo) { | |
return numOne; | |
} else { | |
return numTwo; | |
} | |
} | |
min(0,10); | |
// Recursion | |
// Testing if a number is even | |
function isEven(number){ | |
if (number === 0) { | |
return true; | |
} else if (number === 1) { | |
return false; | |
} else { | |
return number > 0 ? isEven(number-2) : isEven(number+2); | |
// continues to add or subtract from the passed number until it is either 0 or 1, then returns true or false | |
} | |
} | |
isEven(10); | |
// Bean Counting | |
function countChar(str, char){ | |
count = 0; | |
for (var i = 0; i < str.length; i++){ | |
str.charAt(i) === char ? count++ : count | |
} | |
return count; | |
} | |
countChar("kakkerlak", "k"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment