Last active
May 26, 2019 19:38
-
-
Save ivanteoh/c0953ab9ba3fd8d39f29 to your computer and use it in GitHub Desktop.
My answer for exercise in Eloquent JavaScript Second Edition
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
/* | |
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 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 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 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 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 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
Solution for second Fizz Buzz excercise
// print numbers from 1 to 100
for ( var i = 1; i <= 100; i = i + 1 ){
};