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 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 |
Hey! Did you see the author's solution to each? I solved the first the same as you, but when I saw the book's solution, I was like "oh, duh!"
for (var line = "#"; line.length < 8; line += "#")
console.log(line);
EloquentJS_SE0302.js
isEven = function(number){
if(number%2 == 0 ){
return true ;
}
else{
return false ;
}
var checkSign = function(number){
if (Math.sign(number) == -1);
return number * -1
}
};
isEven(-1)
==> false
Solution for second Fizz Buzz excercise
// print numbers from 1 to 100
for ( var i = 1; i <= 100; i = i + 1 ){
// if modulo of 3 is zero and modulo of 5 is zero then print "FizzBuzz"
if ( i % 3 === 0 && i % 5 === 0) {
console.log ("FizzBuzz");
}
// when modulo of 3 is Fizz
else if( i % 3 === 0){
console.log("FIzz");
}
// When modulo of 5 is zero
else if(i % 5 === 0){
console.log ("Buzz");
}
// just print number if its not divisible by 3 and 5
else {
console.log (i);
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man