First of all… as a complete newbie to coding, I found these programming exercises challenging.
If anyone could help explain exercise 3. Chessboard (see questions below), I would appreciate it!
//Exercise 1. Triangle
for(let line = “#”; line.length <8; line += “#”)
console.log(line);
//Exercise 2. Fizzbuzz
for(let num = 1; num <=100; num++)
if(num % 3 == 0 && num % 5 == 0) {
console.log(“FizzBuzz”)
}
else if(num % 3 === 0) {
console.log(“Fizz”)
}
else if(num % 5 === 0) {
console.log(“Buzz”)
}
else {
console.log(num)
}
// Exercise 3. Chessboard
let size = 8;
let board = “”;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x+y) % 2 == 0) {
board += " ";
}
else {
board += "#";
}
}
board += “\n”;
}
console.log(board);
Questions\ regarding ‘Chessboard’ I couldn’t find answers to…
A. Why is there a \n at the end of the code? I don’t understand what role this new line is playing and how it fits in with the rest of the code above it.
B. Please help explain: board += " ";
How is the above ensuring that a space is filled in across the row or down the column all the way until the end of the grid?
Thankyou in advance!