Chapter 2 Exercises

FizzBuzz v1.1

do{
if(num%3 !==0 && num%5 !== 0)
{
console.log(num)
}

num++;

if(num%3==0 && num%5==0)
{
console.log(“FizzBuzz”);
}

if (num%3==0 && num%5!==0)
{
console.log(“Fizz”);

}
if (num%5==0 && num%3!==0)
{
console.log(“Buzz”);
}

}while(num < 100)

FizzBuzz
Looking at other solutions may not be the most elegant but it works. I added the row number to double check. Feedback is welcome. I’m still unclear on what the expression: “row1 < num_rows1” does exactly.

      <script>
var num_rows1 = 101
for (var row1 = 0; row1 < num_rows1; row1 ++){
  var toPrint1 = row1

  if (row1 % 3 == 0) {
    toPrint1 = row1 + " Fizz/3";
  }

  if (row1 % 7 == 0) {
    toPrint1 = row1 + " Buzz/7";
  }

  if (row1 % 3 == 0 && row1 % 5 == 0) {
    toPrint1 = row1 + " FizzBuzz/3&5";
  }

   console.log(toPrint1);

}

“ChessBoard”
I tweaked my “chessboard” and now I understand how those graphics made of ASCII characters. How much work it must be to draw a face!:stuck_out_tongue_winking_eye:

<script>
let height = 20;
let width = 50;
let board = "";

    for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
      if ((x + y) % 5 == 0) {
            board += " ";
      } else {
            board += "#";
      }
}
    board += "\n";
}
console.log(board);
</script>
1 Like

Ok, lets try this again.
Triangle:
var numRows= 7;
for(var row = 0; row <numRows; row++) {
var toPrint = “#”;
for(var column = 0; column<row; column++){
toPrint += “#”;
}
console.log(toPrint);
}

Fizz Buzz:
for(i=1; i <= 100; i++) {
if (i % 3 == 0){
console.log(“fizz”);
}
if (i % 5 == 0) {
console.log(“buzz”);
}
if ((i % 3 == 0) && (i % 5 == 0)) {
console.log(“fizzbuzz”);
}

if ((i % 3 != 0) && (i% 5 != 0)) {
console.log(i);
}

}
Chessboard:
var rows = 8;
var columns = 8;
var sign = " ";
for(i=1; i= <=rows; i++) {
for(var row=1; row <= colums, row++){
if((sign + row) % 2 ==0) sign += " "; else board += “#”:wink: board += “\n”
}
console.log(sign);

The last one won’t print correctly. Where are my errors?

Triangle

let row = 7;
let toPrint =’#’;
for (i = 0 ; i < row ; i++) {
console.log(toPrint);
toPrint+=’#’;
}

FizzBuzz

let number = 100;

for (i = 1 ; i <= number ; 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

let size = 8; //board 8X8
let pattern = ‘#’;
let pattern1 = ’ #’;
let pattern2 = '# ';
let board = ‘’;

for (i=1 ; i <= size ; i++) {

pattern = pattern1

if (i%2 == 0) {
	pattern = pattern2;
}

for (j=1 ; j <= size ; j++) {

	board+= pattern;
     

}
board +='\n';

}
console.log(board);

ChessBoard v1.0

var space = " ";
var line = "# # # # ";

for(var rows = 0; rows <8; rows++)
{
if(rows%2==0)
{
console.log(line);
}
else
{
console.log(space+line);
}
}

Can someone explain why this line is in the code…

let board = “”;

There isn’t even a character there.

You create an empty string, which you add to later on.

Looping A Triangle

var row = 0;
var maxrow = 7;
var toPrint = "#";
while(row < maxrow){
    console.log(toPrint);
    toPrint += "#";
    row++;
  }

FizzBuzz

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

CheeBoard

  var maxrow = 8;
  var maxcolumn = 8;
  var toPrint = "";

  for (var row = 0; row < maxrow; row++){
    for (var column = 0; column < maxcolumn; column=column+2){
      if (row % 2 == 0){
        toPrint +=" #";
      } else toPrint +="# ";
    }

    console.log(toPrint);
    toPrint = "";
  }

Triangle

var output = "";
var outputLength = 7;
while(output.length < outputLength){
  output = output + "#";
  console.log(output);
}

FizzBuzz

for(var i = 1; i < 101; i++){
  var output = "";
  if(i%3 == 0){
    output += "Fizz";
  }
  if(i%5 ==0){
    output += "Buzz";
  }
  else if(output.length == 0){
    output = i;
  }
  console.log(output);
}

Chessboard

var dimension = 8;
for(var row = 0; row < dimension; row ++){
  var output = "";
  for(var column = 0; column < dimension; column ++){
    if(row%2==0){
      var evenChar = " ";
      var oddChar = "#";
    }
    else{
      var evenChar = "#";
      var oddChar = " ";
    }
    if(column%2==0){
      output += evenChar;
    }
    else{
      output += oddChar;
    }
  }
  console.log(output + "\n");
}

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

}
}

Anyone came up with their own code for the ChessBoard?

The ones I am seeing above just look like modifications of the answer in the book.
Surely someone found another way of doing it.

Hey ybaroudi. My understanding of the for loop parameters is that you define the starting number:

for (var row1 = 0;

Then you say, while this is less than the number of rows

row1 < num_rows1;

increase the value of row1

row1++;)

Hope this helps :slight_smile:

I did it like this, is it wrong?

var star = “#”;
for (var i = 0; i < 10; i++) {
star += “#”;
console.log(star);
}

Looks fine. Everyone wrote it a bit different but as long as the triangle is drawn…

1 Like

Triangle Loop

for (var count = 0; count < 7; count++)
{
var symbol = “#”;
for (var tick = 0; tick < count; tick++)
{
symbol = symbol + “#”;
}
console.log(symbol);
}

FizzBuzz Loop

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

ChessBoard

let size = Number(prompt(“Pick a number”));
let line = “”;
for (var row = 0; row < size; row++)
{
for(var box = 0; box < size; box++)
{
if ((row + box) % 2 == 0)
{
line += " ";
}
else
{
line += “X”;
}
}
line += “\n”;
}
console.log(line);

Exercise 1.

 <script>


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

}


</script>

Exercise 2.

for (i=1; i<=100; i++){
	
	      if (i%3==0){
	
	        if(i%5==0){
	          //If it is both print FizzBuzz
	          console.log ("FizzBuzz");
	        }
	
	        else {
	        //If it is divisible by 3 than do this code
	        console.log ("Fizz");
	        }
	
	      }
	      else if (i%5==0){
	        //If it is divisible by 5 and not 3 do this code
	        console.log ("Buzz");
	      }
	      else (console.log (i));
	
	
	    }

Exercise 3.

   for (var row=1; row<=8; row++){
 	      var toPrint = "";

 	      for (var col=1; col<=8; col++){

 	        if (row%2==0){
 	            //If the row is even start with a black square
 	            if (col%2==0){
 	              //If the column is even, white square...
 	              toPrint+=" ";
 	            }
 	            //If only the row is even, black square...
 	            else(toPrint+="#");
 	        }
 	        //If the row is odd start with a white square
 	        else if (col%2==0){
 	          //If the column is even, black square...
 	          toPrint+="#";
 	        }
 	        else (toPrint+=" ");
 	        //If only the row is even, white square...
 	      }

 	      console.log (toPrint);
 	      //Print the line to the console

 	    }

The quickest method i can see is to use the For statement

// Shorthand version

for (let hashstring="#";hashstring.length<8;hashstring +="#"){ console.log(hashstring); }

// Longhand version

var hashstring = “#”; for(i=0;hashstring.length<7;i++){
console.log(hashstring);`
hashstring += “#”;
}

You can also carry out the some solution using a While loop as follows -

var hashstring = “#”;
while(hashstring.length<7){
console.log(hashstring);
hashstring += “#”;
}

And also a do…while loop as follows -

var hashstring = “#”;
do{
console.log(hashstring);
hashstring += “#”;
}
while (hashstring.length < 7);

Fizz Buzz

First solution that creates the output of fizz OR Buzz or Number using if…else statements

for(i=1;i<101;i++){
if(i%3===0){
console.log("fizz ");
}else if(i%5===0){
console.log("buzz ");
}else
{
console.log(i);
}
}

I have used 2 methods of the fizzbuzz solution as follows, the first uses if…else statements

for(i=1;i<101;i++){
if(i%3===0){
if(i%5===0){
console.log("fizzBuzz ");
}else {
console.log("fizz ");
}
}else if(i%5===0){
console.log("buzz ");
}else{
console.log(i);
}
}

The second method uses the “continue” method to restart the loop at the next iteration once a statement is true.

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

Chessboard

For the Chessboard excersize i have used nested loops and if…else statements to check if the line number is odd or even to change the order of the spaces and # characters, changing the variable “boardsize” value allows to make chessboards of different required sizes.

var boardsize=8;
for(i=0;i<boardsize;i++){
for(j=0;j<boardsize;j++){
if(i%2===0){
if(j%2===0){boardoutput +=" “; }else{boardoutput +=”#";}
if(j===boardsize-1){boardoutput +="\n";}
}else{
if(j%2===0){boardoutput +="#"; }else{boardoutput +=" “;}
if(j===boardsize-1){boardoutput +=”\n";}
}
}
}
console.log(boardoutput);

Had fun coding that. Wow, that many different answers, most of them way better than mine :slight_smile:

//Looping a triangle

  let counter = 7;
  let rootsVar = "";
  
  while (counter > 0){
    counter--;
    rootsVar += "#"
    console.log(rootsVar);
  };

  //FizzBuzz

  let fb_num = 0;
  while (fb_num < 100){
    fb_num++;
    if (fb_num % 5 == 0 && fb_num % 3 == 0) {
      console.log("FizzBuzz");
    }
    else if ((fb_num % 3) == 0) {
      console.log("Fizz");
    }
    else if ((fb_num % 5) == 0) {
      console.log("Buzz");
    }
    else {
      console.log(fb_num);
    }
  };

  // Chessboard

  let chessWidth = 20;
  let chessHeight = 5;
  let fieldCount = 0;
  let rowCount = 0;
  var printRowOdd = "";
  var printRowEven = "";

  while (fieldCount < chessWidth) {
    if (fieldCount % 2 != 0) {
      printRowOdd += "#";
      printRowEven += " ";
    }
    else {
      printRowOdd += " ";
      printRowEven += "#";
    }
    fieldCount++
  }

  while (rowCount < chessHeight) {
    if (rowCount % 2 == 0) {
      console.log(printRowOdd);
    }
    else {
      console.log(printRowEven);
    }
    rowCount++
  }

Triangle Loop
let string = “”;
for(let i = 0; i < 7; i++) {
string += “#”;
console.log(string);
}

FizzBuzz Loop
let result = “”;
for(let i = 1; i < 101; i++) {
if (i % 5 == 0) result = “Buzz”;
if (i % 3 == 0) result = “Fizz”;
if ((i % 5 == 0) && (i % 3 == 0)) result = “FizzBuzz”;
if(!result) result = i;
console.log(result)
result = “”
}

Chessboard
let pair = " # # # #";
let impair = "# # # # ";
for(let i = 0; i < 8; i++) {
if(i % 2 == 0) {
console.log(pair);
} else {
console.log(impair);
}
}