for (let i = 1; 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);
}
3- Chess - I couldn` t make it, can some one please explain me more, Thanks
Hi @Howmoney.work good that you tried to find a solution on your own. That is how you learn programming And could you please post your coding answer as code format? take a look at the picture below.
/*Here are solutions to exercises.
I had trouble figuring out chessboard, so I pasted solution provided by book and included my thought process and questions regarding it below. Any feedback would be greatly appreciated.*/
//FIZZBUZZ
for (var numb = 1; numb <=100; numb++) {
if(numb % 3 === 0 && numb % 5 === 0) {
console.log (“FizzBuzz”);
}
else if(numb % 3 === 0) {
console.log(“Fizz”);
}
else if (numb % 3 >=1 && numb % 5 === 0) {
console.log (“Buzz”);
}
else { console.log (numb);
}
}
//CHESSBOARD
let size = 8; //will be used to define rows/columns
let board = “”; //undefined as this will vary
/*First two lines below set starting points for x & y and tell to increase by 1 with each loop until it’s reaches size “8.” Then the remaining code appears to target even & odd numbered places to either log a hash (#) or space( " ") to configure the pattern with a newline break after each symbol. What I’m not sure of is… if variable x & y both start at 0 and are added together then divided by two to determine if place is even or odd, wouldn’t that always yield an even number? I’m not sure how to visualize this…*/
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);
Hello @zinovsky,
You can scroll through this thread in the forum and get plenty amount of answers supplied by our fellow students. I have attached one student’s approach below. Please feel free to take inspiration from anyone of these.
If you have any other questions or doubts , please feel free to reach out. We are here to help always !
Hi @Fati, Hope you’re doing well. Let’s see here, I believe @Howmoney.work also had a similar problem. Do not worry folks, we are here to make it easy for you.
To understand this problem, we can imagine it as a 2D matrix with columns and rows similarly like the example below.
Now, imagine the rows are represented by variable “row” and column “column”.
When we add both the loops together, the code does something like this –
The first loop is for the rows, the second loop is for each column of the row.
First Input
The would have row =1, column =1, therefore 1+1=2 which is EVEN - Therefore- SKIP ! (by giving empty string). Second Input
The second input would have row=1,column=2, therefore 1+2=3 i.e. ODD-- Therefore -> “#” Third Input
.
.
.
.
So on…
This way row by row, we fill the data in alternative boxes.
Hope this gives you a visualisation on how these loops work.
Please feel free to reach out if you have any more questions.
Thank you, Malik and @Howmoney.work, this combo response was extremely helpful!
The animated excel sheet and the explanation below cleared it up for me, thanks!!
First Input
The would have row =1, column =1, therefore 1+1=2 which is EVEN - Therefore- SKIP ! (by giving empty string). Second Input
The second input would have row=1,column=2, therefore 1+2=3 i.e. ODD-- Therefore -> “#”
I also played with the while loop. I managed it for the FizzBuzz, but for hashtags I couldn’t figure out how to break it with the .length keyword, it always went infinite for me.
I struggled quite a bit for the checkerboard. I understood straight away that I need a loop in a loop but struggled to arrange them properly.
Here’s the easy way (only height adjustable):
In the book they mostly use let. Ivan, however, uses var. So far, we don’t know the difference between them but I found out that this doesn’t work with let:
It seemed to ignore the else statement and print eight if.
With that sorted, I moved on to make also the width of checkerboard adjustable and here’s how I managed to do it:
Here, also the let didn’t work for me but var did.
I googled it and here’s the shortest answer I found:
So apparently, my lets under if and else didn’t work outside them so the program just took the first one it could use and executed it every time. I guess this is what they mean in the beginning of the book by saying that whatever you give, Javascript always tries to do something. I look forward to get a deeper understanding of this in the further course.
let number = 0;
undefined
for( let number = 0; number <= 100; number++){
console.log(number);
if( number % 3){
console.log(“Fizz”);
}else if (number % 5){
console.log(“Buzz”);
}
}
The FizzBuzz was the easiest to understand in the mind or to know what to do but the hardest to get translated to code for me. It still has not left my mind yet, lol. Studying different sources and mediums plus the forum helped tremendously. I’m not so sure about the indentation “flaws” and “inefficiency” patterns an adept programmer might locate.
Love how you wanted to dig deeper and find out the peculiarities of this language. Kudos to you !
Yes, the keyword let allows you to make variables that are scoped in the enclosing brackets only. Whereas, var allows you to scope it in the entire function regardless of brackets. That’s why you can access var variables inside the for loops or if/else blocks.
Hi @welowy, Appreciate your effort in writing the code, however, the code provided does not yield to the correct answer. Can you try again or take inspiration from one of the answers in the above thread ?
Also remember to paste the code in code format so that we can easily read your answer.
for (var i=1; i < 10; i++){
if (i % 15 == 0) document.write("FizzBuzz <br>");
else if (i % 3 == 0) document.write("Fizz <br>");
else if (i % 5 == 0) document.write("Buzz <br>");
else document.write(i + "<br>");
}
var size = 8;
var board = "";
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
if ((x + y) % 2 == 0)
board += " ";
else
board += "#";
}
board += "<br>";
}
document.write(board);