Skip to content

Instantly share code, notes, and snippets.

@dansayo
Last active October 14, 2018 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansayo/cdb43a7dd7d045a9319f07557e97e0dd to your computer and use it in GitHub Desktop.
Save dansayo/cdb43a7dd7d045a9319f07557e97e0dd to your computer and use it in GitHub Desktop.
Javascript exercises answered from http://eloquentjavascript.net/index.html
// prints out a right triangle made out of hash tags
let output = "";
for (x = 1; x < 8; x = x + 1) {
output += "#";
console.log(output);
}
// if divisible by 3, Fizz, if by 5, Buzz, if both FizzBuzz;
// otherwise just print the number
for (let i = 1; i < 101; i = i + 1) {
output = "";
if (i % 3 == 0) output = "Fizz";
if (i % 5 == 0) output += "Buzz";
if (output) console.log(output);
else console.log(i);
}
// draw a checkered board with given height and width
const board = function(height,width) {
let square = "";
let board = "";
for(let h = 0; h < height; h = h + 1) {
if (h%2) square="#"; else square=" ";
line = "";
for(let w = 0; w < width; w = w + 1) {
line += square;
if (square=="#") square=" "; else square="#";
}
line += '\n';
board += line;
}
return board;
}
console.log(board(8,8));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment