Chapter 2 Exercises

Blockquote
I did it in a different way than the solution. What does this line means?
board += “\n”;

what’s “\n” ? thanks

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

“\n” is the string for newline or return. It makes the board start the next character on the next line.

1 Like

My code for FizzBuzz was way different than the solution, but it works. The code for the solution is way more elegant and concise than mine:

<script>
    for (let counter = 1; counter <= 100; counter++){
      if(counter % 3 != 0 & counter % 5 != 0){
        console.log(counter);
      } else if (counter % 3 == 0 & counter % 5 != 0){
        console.log("Fizz");
      } else if (counter % 3 != 0 & counter % 5 == 0){
        console.log("Buzz");
      } else {
        console.log("FizzBuzz");
      }};
    </script>
1 Like
  1.   for (p = "#"; p.length < 8; p+= "#"){
          console.log(p);
           }
    
  2.  for (nr=1; nr<=100; nr++){
      let f = "Fizz"
      let b = "Buzz"
       if (nr % 3 == 0 && (nr % 5 == 0)){
          console.log(f + b)
       }
     else if (nr % 3 == 0){
         console.log(f)
      continue;
      }
      else if (nr % 5 == 0){
         console.log(b)
      continue;
      }
      else {
        console.log(nr)
      }
    }
    
  3. for (x = 0; x < 8; x++) { 
          var show = " ";
         for (y = 0; y < 8; y++) {
              if ((x + y) % 2 == 0) { 
              show += " ";
             }
              else {
                    show+= "#";
                 }
              }
          console.log(show);
           }
1 Like

Excellent answer sir.
A quick suggestion. In the above code, you might want to keep the console logs in the same line to avoid creating empty rows. It can be done like this – console.log(" " + L1);

Hi, @jonsax, Love that you’re mixing two concepts to bring in new methods to solve the same problem. Keep up the awesome work!

Happy Learning :slight_smile:

2 Likes

Hi @enrico check the spelling of length on first one.

1 Like

thanks for the suggestion :grinning:

1 Like

oh thanks! I didn’t get that the html commands can be used also for java!

1 Like

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

1 Like

HI @Fati could you post your code as coding format? take a look at the picture below.

2 Likes

FizzBuzz Loop:

This Is A Great Website

FizzBuzz Exercise

<script>

  // Description:

    // This program uses console.log to print number from 1 - 100. For number divisble by 3, print
    // "Fizz" and for number divisble by 5, print "Buzz". For number divisble by both 3 and 5, print
    // "FizzBuzz".


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

  </script>
1 Like

1- Triangle

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- FizzBuzz

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

type or paste code here
1 Like

Triangle loop:

  var num_rows = 7;
  for(row = 0; row < num_rows; row++) {
    var toPrint = "#";

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

Fizzbuzz:

  var num_rows = 100;
  for(row = 1; row <= num_rows; row++) {
    var print = row;

      if (row % 3 == 0 && row % 5 == 0)
      console.log("FizzBuzz");

      else if (row % 3 == 0)
        console.log("Fizz");

      else if (row % 5 == 0)
        console.log("Buzz");

      else
        console.log(print);
  }

Chessboard:

var size = 8;
var board = "";

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

  if ((row + column) % 2 == 0) {
    board += " ";
  }
  else {
    board += "#";
  }
}
board += "\n";
}

  console.log(board);

With this one, I struggled quite a bit and just ended to look at the solution and try to understand it.

1 Like

Hi @Howmoney.work good that you tried to find a solution on your own. That is how you learn programming :grinning: And could you please post your coding answer as code format? take a look at the picture below.

Thank you,
Abel

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


1 Like

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 !

Happy Learning :slight_smile:

2 Likes

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.

rows-and-columns

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 –

chess exampke

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.

Regards.

Happy Learning :slight_smile:

6 Likes
This is a great website

This is the title - No kidding

//write exercise codes

//Looping a Triangle

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

//FizzBuzz exercise

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 exercise

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


  </script>

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 -> “#”

1 Like

I went through the first two exercises rather quickly, Here’s the pyramid:

image

Here’s the FizzBuzz:

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

image

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:

image

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:

image

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.

1 Like

FizzBuzz

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