Chapter 2 Exercises

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

Looping a triangle
Looping A Triangle

FizzBuzz
FizzBuzz

Chessboard
Chessboard

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.

2 Likes

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 += "#"
      }
       console.log(toPrint);
	}

FizzBuzz

for(var i=1; i < 101; 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);
        }
    }

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

Love how you wanted to dig deeper and find out the peculiarities of this language. Kudos to you ! :grin:

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.

Happy Learning!

2 Likes

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.

Thank you.

Happy Learning :slight_smile:

1 Like
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 + "<br>");
        }
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 += "&nbsp;";
    else
      board += "#";
  }
  board += "<br>";
}
document.write(board);
1 Like

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

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

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

HI @mwollike could you post your answer as code format? please take a look at the picture below

2 Likes

Hi Abuga

Thank you, I didn’t knew that function. I will send them again.

Best regards
Michael

ons. 8. jul. 2020 12.16 skrev Abel Sebhatu via Ivan on Tech Blockchain Academy Forum <[email protected]>:

1 Like
    // Triangle:
    var rows = 7;
  	for (var row = 0; row < rows; row++){
    var hash = "#";
    for(var column = 0; column<row; column++){
  	  hash += "#"
      }
    console.log(hash);
  	}


    // Fizz buzz
    var rows = 100;
    for(row = 1; row <= rows; row++) {
      var text = 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(text);
    }


    // Chess
    for (x = 0; x < 8; x++) {
       var hash = " ";
       for (y = 0; y < 8; y++) {
          if ((x + y) % 2 == 0){
             hash += " ";
          }
          else {
            hash+= "#";
          }
       }
       console.log(hash);
    }


2 Likes

Triangle

rows = 7;
symbol = "#";
line = "";

for (step=0; step<rows; step++ ) {
	line += symbol;
	console.log(line);				
}

FizzBuzz

for (theNumber=1; theNumber<100; theNumber++) {
	if ( theNumber % 5 == 0 && theNumber % 3 == 0 ) {
		console.log("Fizz");
	} else if ( theNumber % 3 == 0 ) {
		console.log("FizzBuzz");
	} else if ( theNumber % 5 == 0 ) {
		console.log("Buzz");
	} else {
		console.log(theNumber);
	}   
}

CheckerBoard

cols = 4;
rows = 8;
oddLine = "# ";
evnLine = " #";
lineType = oddLine;
line = "";

for (colStep = 0; colStep < rows; colStep++) {
	if (lineType == oddLine) {
		lineType = evnLine;
	} else {
		lineType = oddLine;
	}
	for (rowStep = 0; rowStep < cols; rowStep++) {
		line += lineType;
	}
	console.log(line);
	line = "";
}
1 Like