Chapter 2 Exercises

This is the best I could do for the FizzBuzz exercise. Spacing is not as I put it in the console of my web browser, but that doesn’t matter.

for(var i = 1; I <=100; i++){
if (i % 3 == 0 && i % 5 == 0){

console.log ("fizzbuzz";
}
}

for(var i = 1; i <=100; i++){
if(i % 3 == 0{

console.log ("fizz"); 
}
}

for (var i = 1; i <= 100; i++){
if (i % 5 == 0){

console.log ("buzz"); 
}
}

Each set came as undefined, but I saw a number at the end of each one.
fizzbuzz = 6
fizz = 33
buzz = 20

I am very certain that these numbers show the number of times that each word appears in the order of numbers between 1 and 100. For example, "fizzbuzz" is written in place of all numbers divisible by both 3 and 5. It appears six times in the order. Specifically, those numbers would be 15, 30, 45, 60, 75, and 90.

Now I will need assistance with the chessboard exercise. Both of these exercises will have to be in separate posts.

Thank you so much for your assistance!

@jgentry Great Job! but you have to put all the conditions in one loop not in 3 loops. could you fix that? (think of if , else if and else).

1 Like

Okay. I’ll go back into the previous post and fix that. Thanks!

I cannot seem to figure out how to put it all in one loop. I’ve tried using the else and else if conditions but it’s not working. My console says, “Uncaught SyntaxError.” Thanks.

@jgentry ok then, lets do it together

for(var i = 1; i <=100; i++){
//this condition checks if the number is divisible by 3 and 5, if it is  true print "fizzbuzz"  , if not then check next condition 
if (i % 3 == 0 && i % 5 == 0){

console.log ("fizzbuzz");
}
// this  condition checks if the number is divisible by 3, if it is  true print "fizz", if not then go to next condition
else if(i % 3 == 0{

console.log ("fizz"); 
}
// this  condition checks if the number is divisible by 5, if it is true print "buzz", if not then go to next condition
else if(i % 5 == 0{

console.log ("fizz"); 
}
// if all the above condition are false then it prints only the number. 
else{
console.log (i); 
}

}




1 Like

I already had it figured out before you replied. My mistake was that I had if else instead of else if. Here is my edited answer. Once again, thank you!

for(var 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);
}
}

Now I will need help with the Chessboard exercise.
Could you show me how to set it up, please?

1 Like
// FizzBuzz
for (var 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);
 }
}
1 Like

@pepemadrid great answer! but not really correct, for example: in case of number 15 it prints “FizzBuzzFizzBuzz”, it should only print “fizzbuzz”. It would be great if you could fix it :grinning:.

Happy coding,
Abel

1 Like

Right, good you looked into it. Trying to solve the issue …

1 Like

Please show me how to set up the Chessboard exercise. Thank you!

Hi @jgentry,

Please find other student’s answers for inspiration. There are many great answers! :smiley: And also you can find my explanation below for how these loops work for the chessboard exercise.

Hope this helps.

2 Likes

Okay. I’ve been referring to other students’ answers to get help. But in some ways those answers are structured, the console won’t accept them. Anyway, thanks!

I still don’t understand. How am I supposed to set the problem up? The clip and the instructions you gave me barely helped me.

I did my best. I know it does not look like what is described in the book, but it is the best I could do. I just gave up and looked at the correct answer and tried to follow it. I was tired of being stuck on these exercises.

for(var row=0; row<8; row++){
    for(let col = 0; col < row; col++){
        if ((row + col) % 2 == 0){
            board += " ";}
        else{board += "#";
            }
    }
board += "\n";
}
console.log(board)

All the time I’ve been enrolled in the Academy I’ve quickly moved through the courses without even bothering to practice what I’ve learned. Now I’ve been required to slow down and try to put what I learn into practice. That’s not so easy.

Some things are easier said than done. That is especially true for these exercises.

Thank you!

Hi,
I’ve added some comments. Try to relate it with the previous post I linked. Hope this will help too.

var row = 8; // Define number of rows 
var col = 8; // Define number of columns

// Loop over each row
for (var rowNumber = 1; rowNumber < row+1; rowNumber++ ) {
  // Define the string
  var chessboard = “”;
 // Loop over each column for that particular row number
  for (var columnNumber = 1; columnNumber < col+1; columnNumber++) {
     //If you add column number and row number , if the sum is Even then print "#"
     if ((rowNumber + columnNumber)%2==0) {
       chessboard += “#”
     }
   //If you add column number and row number , if the sum is Odd then print " " (empty string)
    else if ((rowNumber+columnNumber)%2 !== 0) {
      chessboard += " "
     }
   }
// Print the chessboard 
console.log(chessboard);
}
1 Like

I’ll go back in and do the exercise again. It was causing me some complications. Thank you!

When I try to put this into my console,

var row = 8;
var col = 8;

it gives me undefined when I press ENTER. Can you please help me?

Don’t worry about any of the posts I have written recently. Your solution actually helped me in solving the Chessboard exercise. Thank you!!!

I suppose I had to do this one in Atom rather than in the console of Google Chrome.

var row = 8;
var col = 8;
for(var rowNumber = 1; rowNumber < row+1; rowNumber++ ){
  var chessboard = "";
  for(var columnNumber = 1; columnNumber < col+1; columnNumber++ ){
    if ((rowNumber+columnNumber)%2==0) {
      chessboard += "#"
    }
    else if ((rowNumber+columnNumber)%2!==0){
      chessboard += " "
    }
  }
  console.log(chessboard);
}
1 Like

Triangle
for (var result = “#”; result.length <=7; result = result + “#”)
console.log(result);

Fizzbuzz
for(number=0;++number<101;console.log(number%5?x||number:x+‘Buzz’))x=number%3?’’:‘Fizz’

Chessboard

var chessBoard= ‘’,

size= 8,

c;

for ( var i = 0 ; i < size ; i++) {

c= i%2 ? ‘# ’ : ’ #’;

for ( var j = 0 ; j < size/2 ; j++) {

chessBoard+= c;

}

chessBoard+= ‘\n’;

}

console.log(chessBoard);

@jgentry, Fantastic ! :smiley:
Console is usually used for short functions or statements to play around with. When you want to write relatively big or complex code, it is always recommended to use IDE’s such as Atom or VS Code.

Happy Learning ! :slight_smile:

1 Like