Chapter 2 Exercises

There are multiple issues with your syntax here. I would suggest you redo your course videos to have strong understanding of fundamentals.

Issue 1 -

This is incorrect syntax for taking two inputs from user. This will only give one prompt but you require two.

Issue 2 -
You did not console log your string variable, therefore you do not see any console outputs. Console log the string variable after the while loop.

Issue 3 -

Spelling of length is incorrect.

Please try to cement your fundamentals and try these exercises again.

Happy Learning!

1 Like

FizzBuzz

for (var a = 0; a<= 100; a ++){
if (a % 3 === 0)
{ console.log(“Fizz”);
} else if (a % 5 === 0)
{ console.log(“Buzz”);
} else if (a % 3 === 0 && a%5 === 0)
{ console.log(“FizzBuzz”);
} else
{ console.log(a);
}
}

Chessboard

let size = 8;
let chessBoard = “”;

for (let y = 0; y < size; y ++) {
for (let x = 0; x < size; x ++) {
if ((y+x) % 2 === 0){
chessBoard += " ";
} else {
chessBoard += “#”;
}
}
chessBoard += “\n”;
}
console.log(chessBoard);

I stumbled across @Palmeguppsi breakdown for Chessboard. Thank you so much for doing that! I was so lost but I now understand.

1 Like

FizzBuzz Loop (first attempt):
for(let i = 0; i <= 100; i=i+1)

{
if (i % 3)
{
console.log(‘Fizz’); continue
}
console.log(i);
}

Try it yourself lol

it’s easier to crash the browser than I thought :rofl:

1 Like

FizzBuzz ~

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

{ console.log(‘Fizz’); continue }

if (i % 5 === 0)

{ console.log(‘Buzz’); continue }

console.log(i);
}

1 Like
  1. var num_rows = 7
    for (var row = 0; row < num_rows; row++){
    var toPrint = “#”;

    for (var column = 0; column<row; column++){
    toPrint += “#”
    }
    console.log(toPrint);
    }

  2. for (let i = 1; i <= 100; i++) {
    let output = “”;
    if (i % 3 == 0) output += “Fizz”;
    if (i % 5 == 0) output += “Buzz”;
    console.log(output || i);
    }

  3. I couldn’t figure this out so have not written an answer - i looked on the answer page.

1 Like
  1. Triangle

for (let line = “#”; line.length < 8; line += “#”)
console.log(line);

  1. FizzBuzz

var toPrint = 0;
for(i = 1; i <= 100; i++){
toPrint = i
if (i%3 == 0 && i%5 == 0) {
toPrint = “FizzBuzz”
}
else if (i%3 == 0 && i%5 != 0){
toPrint = “Fizz”
}
else if (i%5 == 0 && i%3 != 0){
toPrint = “Buzz”
}
console.log(toPrint);
}

  1. Chessboard

var boardDim = 8;
var toPrint = “”;

   for (row = 0; row < boardDim; row++){

     for (col = 0; col < boardDim; col++){

       if ((row + col) % 2 == 0){
         toPrint += " ";

       } else {
         toPrint += "#";
      }
    }
     toPrint += "\n";
   }

console.log(toPrint);

1 Like

FizzBuzz exercise

let num = 1
    for (num; 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)
      }
    }

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);
1 Like
  1. Looping a triangle
    var hashtag = "#";
    var hashtags = "";
    for(var i=0;i<7;i++){
      hashtags += hashtag;
      console.log(hashtags);
    }
  1. FizzBuzz
    var output;
    for(var i=1;i<=100;i++){
      output = "";
      if(i%3==0)
        output += "Fizz";
      if(i%5==0)
        output += "Buzz";
      if(i%5!=0&&i%3!=0)
        output = i;
      console.log(output);
    }
  1. Chessboard
    var hashtag = "#";
    var space = " ";
    var text = "";
    var size = 4;

    for(var i=0;i<size;i++){
      for(var j=0;j<size;j++){
        if(i%2==0){
          if(j%2==0){
            text += space;
          }else{
            text += hashtag;
          }
        }else{
          if(j%2==0){
            text += hashtag;
          }else{
            text += space;
          }
        }
      }
      console.log(text);
      text = "";
    }
1 Like

Triangle
let row = 1;
let print = “#”;
while (row<8) {
console.log(print);
row = row + 1;
print = print + “#”;
}

FizzBuzz – This one could be a bit tricky because of the execution flow. You need to make sure that you either put the condition that a number should print fizzbuzz if it is divisible by 3 and 5 first in the if statements, or explicitly state that Fizz and Buzz should be printed if a number is ONLY divisible by 3 and 5 respectively. I showed the latter in this case.
let row = 1;
let print = “#”;
while (row<101) {
if (row%3==0 && row%5!==0) {
console.log(“Fizz”);
}
else if (row%5==0 && row%3!==0) {
console.log(“Buzz”);
}
else if (row%5==0 && row%3==0) {
console.log(“FizzBuzz”);
}
else {
console.log(row);
}
row = row + 1;
}

Chessboard – This one took a bit of research. To fulfill the last part of the assignment of allowing the chessboard to change in size I made it so you can change the dimension variable to whatever whole number you wanted it to be. I wanted to write it so it prompted a user to add the number for themselves, but I learned that the prompt function is not supported by ATOM. Otherwise, you can turn the dimension variable into a number (from a string) with the Number function and make it so you get a printout based on whatever the user inputs (as long as they add whole non-negative numbers :blush: )

let dimension = 8;
let counter = 1;
let subcounter = 1;
let array = [];
let print = “#”;

while (counter<=dimension) {
if (counter%2!==0) {
for(print; subcounter <= dimension; subcounter++)
{
array.push(print);
}
console.log(" " + array.join(’ ‘));
}
else if (counter%2==0) {
for(print; subcounter <= dimension; subcounter++)
{
array.push(print);
}
console.log(array.join(’ '));
}
counter = counter + 1;
}

1 Like

Can anyone tell me why my whole script goes grey before typing anything?

Also, when I remove it goes back to being the right colours.

image

1 Like

FIZZBUZZ

for (counter = 1; counter<101; counter++) {
if ((counter % 3 == 0) && (counter % 5 == 0)) {console.log(“Fizzbuzz”);}
else if (counter % 3 == 0) {console.log(“Fizz”);}
else if (counter % 5 == 0) {console.log(“Buzz”);}
else {console.log(counter);
}
}

CHESSBOARD
I added some edges to the chessboard.

for (numRows = 0; numRows < 10; numRows++) {
if ((numRows == 0) || (numRows == 9)) {console.log(" ________ “);}
else if (numRows % 2 == 0) {console.log(”|# # # # |");}
else if (numRows % 2 == 1) {console.log("| # # # #|");}
}

1 Like

Its a visual bug from atom, some times it appears without a logic reason, your syntax looks complete ok, but still you are facing the bug, i just advice you to not care to much about it, it will show the colors again if you keep coding on it, is just a visual bug.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Looping a triangle

var num_rows = 7;
for(var row = 0; row < num_rows; row++){
var toPrint = “#”;
for(var column =0; column<row; column++){
toPrint += “#”;
}
document.write(toPrint + “
”);
}

Fizzbuzz

for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
console.log(output || n);
}

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);

1 Like

let i=0;
let x=i%3;
let y=i%5;
let z=i%15
while(i<100){
if(x=0){
console.log(“FIZZ”);
}
else if(y=0){
console.log(“Buzz”);
}
else if(z=0){
console.log(“FizzBuzz”);
}
else{console.log(i);
}
i++;
}

For exercise number 2. This returns me only all the numbers from 0 to 100. Could anyone help me debug this? I dont understand what Im doing wrong here:(

  1. Triangle
    for (let line = “#”; line.length < 8; line += “#”)
    console.log(line);

2.Fizzbuzz
for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
console.log(output || n);
}

  1. 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);
1 Like

Looping a triangle:

  let num_rows=7;
  for (let row=0;row<num_rows;row++) {
    let toPrint="#";
    for (let column=0; column<row; column++) {
      toPrint+="#";
    }
    console.log(toPrint);
  }

FizzBuzz:

  for (let i = 0; i < 100; i++) {
    if ((i+1)%3==0&&(i+1)%5==0) {
      console.log("FizzBuzz");
      continue;
    }
    if ((i+1)%3==0) {
      console.log("Fizz");
      continue;
    }
    if ((i+1)%5==0) {
      console.log("Buzz");
      continue;
    }
    console.log(`${i+1}`);
  }

Chessboard:

  let width=parseInt(prompt("Enter the width"));
  let height=parseInt(prompt("Enter the height"));
  let text="";
  for (let i = 0; i < height; i++) {
    if ((i+1)%2==0) {
      for (let i = 0; i < width; i++) {
        text+="# ";
      }
      console.log(text);
      text="";
    } else {
      for (let i = 0; i < width; i++) {
        text+=" #";
      }
      console.log(text);
      text="";
    }
  }
1 Like

I think this resolve the problem:

let i=0;
while(i<100){
let x=(i+1)%3;
let y=(i+1)%5;
if (x==0 && y==0) {
console.log(“FizzBuzz”);
}
if(x==0&&y!=0){
console.log(“FIZZ”);
} else if(y==0&&x!=0){
console.log(“Buzz”);
} else if(x!=0&&y!=0) {
console.log(${i+1});
}
i++;
}

1 Like

Triangle Loop:

var num_rows = 7;

for (var row = 0; row < num_rows; row++) {

var toprint = “#”;

for(var column = 0; column < row; column++) {

toprint += “#”; }

console.log(toprint);

}

FizzBuzz Loop:

for (let n = 1; n <= 100; n++) {

let output = “”;

if (n % 3 ==0) output += “Fizz”;

if (n % 5 ==0) output += “Buzz”;

console.log(output || n);}

ChessBoard Loop

let size = 8;

undefined

let board = “”;

undefined

for (let y = 0; y< size; y++) {

for (let x = 0; x < size; x++) {

if ((x + y) % 2 == 0) {

board += " "; }

else {

board += “#”;

}

1 Like

Fizz Buzz:

 var i = 1;
      while (i < 101) {

            if (i % 3 === 0){console.log("Fizz");}
            else if (i % 5 === 0){console.log("Buzz");}
            else if (i % 3 === 0 && i % 5 === 0){console.log("FizzBuzz");}
            else {console.log(i);}
      i++;}

Chessboard:

var size = 8;
      var chessboard = "";

      for(columns=0; columns < size; columns ++){
        for(rows=0; rows < size; rows ++){

          if((columns + rows) % 2 == 0)
            chessboard += " ";
          else
            chessboard += "#"
          }
            chessboard += "\n";
      }
      console.log(chessboard)
1 Like