Last active
January 12, 2016 20:32
-
-
Save wesleyterry/a27796a10d469378e92e to your computer and use it in GitHub Desktop.
eloquent javascript exercises
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
for (i = "#"; i.length <= 8; i += "#"){ | |
console.log(i); | |
}; |
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
for (i = 0; i <= 100 ; i++){ | |
if ((i % 3 == 0 ) && (i % 5 == 0)) | |
console.log("FizzBuzz"); | |
else if (i % 3 == 0) | |
console.log("Fizz"); | |
else if (i % 5 == 0) | |
console.log("Buzz"); | |
else | |
console.log(i); | |
} |
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
var size = 8; | |
var board = ""; | |
for (var one = 0; one < size; one ++){ | |
for (var two = 0; two < size; two ++){ | |
if ((one + two) % 2 == 0) | |
board += "#"; | |
else | |
board += " "; | |
} | |
board += "\n"; | |
} | |
console.log(board); |
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
function min(sum1,sum2){ | |
if (sum1 < sum2) | |
return sum1; | |
else | |
return sum2; | |
} | |
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
function isEven(N){ | |
if (N % 2 == 0) | |
return true; | |
else | |
return false; | |
}; | |
console.log(isEven(7)); | |
// the correct solution using recursion, even though mine works. | |
function isEven(N){ | |
if (N == 0) | |
return true; | |
else if (N == 1) | |
return false; | |
else if (N < 0) | |
return isEven(-N); | |
else | |
return isEven(N - 2); | |
}; |
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
function countChar(string, ch) { | |
var counted = 0; | |
for (var i = 0; i < string.length; i++) | |
if (string.charAt(i) == ch) | |
counted += 1; | |
return counted; | |
} | |
function countBs(string) { | |
return countChar(string, "B"); | |
} | |
console.log(countBs("BeerBrat")); | |
// → 2 | |
console.log(countChar("wesley", "e")); | |
// → 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment