Looping a Triangle
This is what I came up with; but after seeing the answer, I like the answer better.
var numRows = 7
var hash = ""
for (var row = 0; row < numRows; row += 1){
hash += "#";
console.log (hash)
}
FizzBuzz
This is what I figured out. I suppose its more than the exercise demands, but my code allows for easier changes. What if you wanted to change the divisors or the range or the increments or FizzBuzz to something different like OnkoBonko? And I’m not sure if I’m supposed to put else in it because it works fine with just the if statements. And what are the words like “if” called? Keywords?
let range1 = 1
let range2 = 100
let increment = 1
let num1 = 3
let num2 = 5
for (counter = range1; counter <= range2; counter += increment){
if (counter % num1 == 0 && counter % num2 != 0) console.log ("Fizz");
if (counter % num2 == 0 && counter % num1 != 0) console.log ("Buzz");
if (counter % num1 == 0 && counter % num2 == 0) console.log ("FizzBuzz");
if (counter % num1 != 0 && counter % num2 != 0) console.log (counter);
}
Chessboard
I couldn’t figure out this one. Even after seeing the answer, I’m still not sure what exactly is going on here.
let size = 8
let hash = ""
for (y = 0; y < size; y += 1){
for (x = 0; x < size; x += 1){
if ((x + y) % 2 == 0){
hash += " ";
} else {
hash += "#";
}
}
hash += "\n";
}
console.log (hash)