Chapter 2 Exercises

Hi @NicoG,

The FizzBuzz exercise is spot on! However, chessBoard exercise seems to have a lot of issues. These issues include spelling mistakes, syntax error, missing brackets etc.

Please could you redo the exercise, if you’re stuck you can take inspiration from the answers above.

Hope this helps. Please feel free to reach out if there’s anything else you need.

Happy Learning! :slight_smile:

2 Likes

      //Triangle of #'s
      for(var row = 0; row < 7; row++){
        var displayTrangle = "#";
        for(var col = 0; displayTrangle.length < row; col++){
          displayTrangle += "#";
        }
        console.log(displayTrangle);
      }


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

      //Chessboard
      var gridSize = 8;
      var printChessboard = "";
      for(var row = 1; row < gridSize+1; row++){
        for(var col = 1; col < gridSize+1; col++){
          if(row % 2 == 0 && col % 2 !== 0 || row % 2 !== 0 && col % 2 == 0) printChessboard += "#";
          else printChessboard += " ";
        }
        printChessboard += "\n";
      }
      console.log(printChessboard);
1 Like
//Looping a Triangle
var hash = "#";
var strng = hash
while (strng.length < 7){
console.log(strng);
strng = strng + hash;
}

//Fizzbuzz
var num = 1;
while (num < 101){
  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);
  }
num++;
}

//Chessboard
var white = "   ";
var black = "#  ";
var num = 0;
var size = 8;
var line1 = black;
var line2 = white;

//construct line1
for (x=1;x<size;x++){
    if (x%2==0){
      line1 += black;
    }
    else{
      line1 += white;
    }
}
//construct line2
for (x=1;x<size;x++){
    if (x%2==0){
      line2 += white;
    }
    else{
      line2 += black;
    }
}

//construct grid
while (num < size){
   if (num%2==0)    {
     console.log(line1);
   }
   else             {
     console.log(line2);
   }
   num++;
}
1 Like

// Exercise 1

  var num_rows = 7;
     for (let num = 0; num < num_rows; num++){

       var toPrint = "#";

         for(let col=0; col<num; col++) {
           toPrint += "#";
         }
           console.log(toPrint);

       }


// Exercise 2

  for (let num = 1; num<101; num++)
   {

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

//Exercise 3

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

     if(count%2 !=0){

       console.log(" "+ "# # # #  \n");
       //document.write("<br>");
       //console.log("\n");

     }

     else if (count %2 ==0) {

       console.log("# # # #  \n");
       //document.write("<br>");
       //console.log("\n");
     }

   }

 </script>

1 Like

EXCERCISES

1)/ PYRAMID LOOP

createPyramid(7) // pass number as row of pyramid you want.

function createPyramid(rows)
{
    for (let i = 0; i < rows; i++) {
        var output = '';
        for (let j =0; j < rows - i; j++) output += ' ';
        for (let k = 0; k <= i; k++) output += '₿';
        console.log(output);  
    } 
}
1 Like

2). BIT BUZZ LOOP

 var bitbuzz = function(start,stop) {
    for (var x=1;x <= stop; x++)
        var string =',';
    if (x%3 == 0) {
            string += '₿';
    }
    if (x%5 ==  0){
        string += 'Buzz';
    }
    if (x%5 && x%3){
        string += 'bitbuzz';
    }
    return string;
};

CHESSBOARD LOOP

 var gridBoard = "";
    var size = 8;

    for (var lineCounter = 1; lineCounter < size; lineCounter++) { 

        if (lineCounter%2 === 0) {
 //if lineCounter is an even number
        for (var charCounter = 1; charCounter < size; charCounter++) {
            var evenOdd = (charCounter%2 === 0);
            switch (evenOdd) {
                case true:
                    (gridBoard += "#");
                    break;
                case false:
                    (gridBoard += " ");
                    break;
                }
            }                   
        }
    else { //if lineCounter is an odd number
        for (var charCounter = 1; charCounter < size; charCounter++) {
            var evenOdd = (charCounter%2 === 0);
            switch (evenOdd) {
                case true:
                    (gridBoard += " ");
                    break;
                case false:
                    (gridBoard += "#");
                    break;
            }
            }                       
        }   
    gridBoard += "\n";
    }
    console.log(gridBoard);
1 Like

Here is the entire code.
Completed the tasks and made the page look like binary (Tasks are displayed in the console)
Each task is displayed as one text, this is a design choice.

<!doctype html>
<html>

    <head>
        <title>Binary</title>

        <script>
            //Display toggle
            var showBinary = true;
            var showTriangle = true;
            var showChessboard = true;
            var showFizzBuzz = true;

            //Binary display
            //Environment
            var textHeight = 100;
            var textWidth = textHeight * textHeight;

            var text = "";

            //Main
            if (showBinary)
            {
                //Format all rows in 2D, under each other
                for (var i = 0; i < textHeight; i++)
                {
                    //Construct row
                    for (var j = 0; j < textWidth; j++)
                    {
                        text += Math.floor(Math.random() * 2);
                    }
                    document.write("<h2>" + text + "<br>" + "<h2>");
                    text = "";
                }
            }

            //Display triangle in console
            //Environment
            var triangleHeight = 7;

            var triangleBody = "#";
            var triangleText = "Triangle:  [h:" + triangleHeight + "]\n";

            //Main
            if (showTriangle)
            {
                //Construct triangle
                for (var i = 0; i < triangleHeight; i++)
                {
                    triangleText += triangleBody + "\n";
                    triangleBody += "#";
                }
                //Output
                console.log(triangleText);
            }

            //Display chessboard in console
            //Environment
            var boardHeight = 8;
            var boardWidth = boardHeight;
            //boardWidth = ;

            var invertPattern = false;

            var chessBoardBody = " #";
            var chessBoardText = "Chessboard:  [h:" + boardHeight + "|w:" + boardWidth + "]\n";

            //Main
            if (showChessboard)
            {
                //Format all rows in 2D, under each other
                for (var i = 0; i < boardHeight; i++)
                {
                    //Construct row
                    for (var j = 0; j < boardWidth/2; j++)
                    {
                        chessBoardText += chessBoardBody;
                    }

                    //Invert board pattern
                    if (!invertPattern)
                    {
                        chessBoardBody = "# ";
                        invertPattern = true;
                    }
                    else
                    {
                        chessBoardBody = " #";
                        invertPattern = false;
                    }
                    chessBoardText += "\n";
                }
                //Output
                console.log(chessBoardText);
            }

            //FizzBuzz
            //Environment
            var maxNumber = 100;

            var number = "";
            var text = "FizzBuzz:  [r:" + maxNumber + "]\n";

            //Main
            if (showFizzBuzz)
            {
                //Construct grid: Number | Number(Design choice), replace numbers that are dividable by 3 w/Fizz and 5 w/Buzz, write FizzBuzz if a number is dividible by both
                for (var i = 1; i < maxNumber+1; i++)
                {
                    number = "";
                    if (i % 3 == 0)
                    {
                        number += "Fizz";
                    }

                    if (i % 5 == 0) //Didnt use else if and no conditional code to write "FizzBuzz" so the program would be as flexible as possible
                    {
                        number += "Buzz";
                    }

                    if (i % 9 == 0) //Added another one to prove my point above
                    {
                        number += "Pluzz";
                    }

                    if (number == "")
                    {
                        number = i;
                    }
                    //Output
                    text += number + " | "; //Makes it look fancy
                }
                console.log(text);
            }

        </script>

    </head>

    <body>



    </body>

</html>
1 Like

Hi @George,
Amazing answer George! However, the Triangle #'s exercise does not yield the right answer. Could you try again, if there’s any help you require please feel free to reach out.

Happy Learning! :slight_smile:

1 Like

Hi @Malik,
Thank you for pointing out, fixed it now:
displayTriangle.length < row
instead of toDisplay.length < row

2 Likes

Greetings @NLUGNER,
Seems like there is an issue in this exercise answer. Please could you redo it. If there are any doubts or concerns, please feel free to reach out. :slight_smile:

Excellent @George! :star_struck:

Happy Learning! :smiley:

1 Like
let size = 8;

let board = "";

for (let y = 0; y < size; y++) {
  for (let x = 0; x < size; x++) {
    if ((x + y) % 8 == 0) {
      board += "";
    } else {
      board += "#";
    }
  }
  board += "\n";
}

console.log(board);

Hi @hminott, This exercise answer seems to not give the right answer. Please try again. If you have any doubts or concerns, please feel free to reach out. :slight_smile:

1 Like

I did struggle with these exercises and being honest, I resisted the temptation to peek at the answers until I looked at the clock and 8 hours had gone by! Fortunately, most of my attempts failed due to a missing bracket or semicolon but my logic and choice of commands was good. Mainly syntax mistakes.

I realized besides learning what each function does, it’s essential to understand the bindings that are part of the language standard because they are hidden, not included with a simple explanation of what a particular function does.

Gonna keep practicing!

1 Like

I really struggled with this exercise. I re-read several parts of the book and looked at various sites after attempting these many times. I am still not confident in these exercises so i am going to go back over the material before proceeding.

Triangle

var numRows = 7;
for(let row = 0; row < numRows; row++){
var printS = ("#");

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

    }

FizzBuzz

for (number = 1; number<100; number++){
if (number % 15 == 0) console.log(“fizzbuzz”);
else if (number % 3 == 0 ) console.log(“Fizz”);
else if (number % 5 == 0) console.log(“Buzz”);
else console.log(number);
}

Chess

let size = prompt("Enter size of the Chessboard: ");
let chess = “”;

for (row = 0; row < size; row++){
for (column = 0; column < size; column++){
    if (column % 2 == 0 && row % 2 == 0){
         chess += " ";
       }
       else if (column % 2 == 0 && row % 2 == 1){
            chess += "#";
        }
        else if (column % 2 == 1 && row % 2 == 1){
            chess += " ";
        }
        else {
            chess += "#";
        }
    }
    chess += "\n";
}
console.log(chess);

Hi @Malik thanks for letting me know. Is this it? if not then can you help me to figure it out please :slightly_smiling_face:? Thanks again.

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

Hey everyone! I completed the two exercises. Took some time and YouTube lol. But in the end I completed them and understand things a lot better. I am really enjoying the class. :smiley:

1 Like

Thanks Mate! Will have a look and go through the exercise. I appreciate your comments

Looping a triangle

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

Fizz Buzz

for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
if (n % 3 && 5 == 0) output += “FizzBuzz”;
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

Triangle Loop

var print = “#”;
var num_row = 8;
while (print.length<num_row){
console.log(print);
print+="#";

FizzBuzz

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

ChessBoard

var size=8;

for(var row=0;row<size;row++){
var string="";
for(var column=0;column<size;column++){
if((column+row)%2==0){
string+="#";
}
else
string+=" ";
}
console.log(string);
}

1 Like