Hi.
So I also managed to successfully complete 3/3 exercises.
So, Looping triangle;
document.write("<p>Excersise 1 - Looping triangle:<br />");
for(let pyramid="#";pyramid.length<7;pyramid+="#"){
document.write(pyramid);
document.write("<br />");
}
document.write("</p>");
FizzBuzz;
document.write("<p>Excersise 2 - FizzBuzz:<br /");
for (let number=0;number<=100;number++){
if(number%3 == 0 && number%5 == 0) document.write(number + " - FizzBuzz - Divisible by 3 and 5");
else if(number%3 == 0) document.write(number + " - Fizz - Divisible by 3");
else if(number%5 == 0) document.write(number + " - Buzz - Divisible by 5");
else document.write(number);
document.write("<br />");
}
document.write("</p>");
and finally Chessboard. I tried to fancy it up a little and do some extra steps to learn and experiment. By looking at oher examples, I see I could have done it much cleaner. The code;
document.write("<p>Excersise 3 - Chessboard:<br />");
let boardLenght=Number(prompt("We're creating a square chessboard. Enter the desired board length - number of fields per side (e.g. enter 8 for board size 8x8)."));
if ((boardLenght > 0) && (boardLenght % 1 == 0) && (boardLenght != NaN)) {
let whiteField=" ";
let blackField="#";
for (let rows=1;rows<=boardLenght;rows++){
if(rows%2 == 0) {console.log(rows +" = even row")} else {console.log(rows +" = odd row")};
if(rows%2 == 0) // if it is an odd row, it should start with a black field
for (let evenRowColumn = 1; evenRowColumn <= boardLenght ; evenRowColumn++) {
console.log("Even row executed: row " + rows + ", column " + evenRowColumn);
if(evenRowColumn%2 == 0) {document.write(whiteField); console.log(" White field")}
else {document.write(blackField); console.log(" Black field")};
}
else // if it is an even row, it should start with a white field
for (let oddRowColumn = 1; oddRowColumn <= boardLenght ; oddRowColumn++) {
console.log("Odd row executed: row " + rows + ", column " + oddRowColumn);
if(oddRowColumn%2 == 0) {document.write(blackField); console.log(" Black field");}
else {document.write(whiteField); console.log(" White field");};
}
document.write("<br />");
};
}
else alert("The number you have entered either isn't a positive number, isn't a whole number or is not a number at all. To try again, please reload the page.");
document.write("</p>");
Thank you.
J.