-
-
Save jessekafor/b5aad6149d229c0907b1 to your computer and use it in GitHub Desktop.
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 answer for exercise 'Looping a triangle' | |
in Eloquent JavaScript Second Edition | |
Chapter 2 Program Structure | |
*/ | |
var note = ''; | |
for (var i = 0; i < 7; i++) { | |
console.log(note += '#'); | |
} |
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 answer for exercise 'FizzBuzz' | |
in Eloquent JavaScript Second Edition | |
Chapter 2 Program Structure | |
*/ | |
for (var i = 0; i < 100; i++) { | |
var number = i + 1; | |
var result = ""; | |
if (number % 3 == 0) { | |
result = "Fizz"; | |
} | |
if (number % 5 == 0) { | |
result += "Buzz"; | |
} | |
if (!result) { | |
result = i + 1; | |
} | |
console.log(result); | |
} |
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 answer for exercise 'Chess board' | |
in Eloquent JavaScript Second Edition | |
Chapter 2 Program Structure | |
*/ | |
var size = 8; | |
for (var i = 0; i < size; i++) { | |
var line = ""; | |
for (var j = 0; j < size; j++) { | |
var total = i + j; | |
if (total % 2 == 0) { | |
line += '#'; | |
} else { | |
line += ' '; | |
} | |
} | |
console.log(line); | |
} |
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 answer for exercise 'Minimum' | |
in Eloquent JavaScript Second Edition | |
Chapter 3 Functions | |
*/ | |
// Your code here. | |
function min(first, second) { | |
if (first < second) { | |
return first; | |
} | |
return second; | |
} | |
console.log(min(0, 10)); | |
// → 0 | |
console.log(min(0, -10)); | |
// → -10 |
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 answer for exercise 'Recursion' | |
in Eloquent JavaScript Second Edition | |
Chapter 3 Functions | |
*/ | |
// Your code here. | |
function isEven(number) { | |
var whole_number = Math.abs(parseInt(number, 10)); | |
if (whole_number === 0) { | |
return true; | |
} | |
if (whole_number === 1) { | |
return false; | |
} | |
return isEven(whole_number - 2); | |
} | |
console.log(isEven(50)); | |
// → true | |
console.log(isEven(75)); | |
// → false | |
console.log(isEven(-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
/* | |
My answer for exercise 'Bean counting' | |
in Eloquent JavaScript Second Edition | |
Chapter 3 Functions | |
*/ | |
// Your code here. | |
function countBs(word) { | |
return countChar(word, "B"); | |
} | |
function countChar(word, character) { | |
var total = 0; | |
for (var count = word.length - 1; count > -1; count--) { | |
if (word.charAt(count) === character) { | |
total += 1; | |
} | |
} | |
return total; | |
} | |
console.log(countBs("BBC")); | |
// → 2 | |
console.log(countChar("kakkerlak", "k")); | |
// → 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment