Chapter 2 Exercises

1.Looping a triangle:
var star=" “;var i=0;while(i<7){star+=”#";i++;document.write(star+"
");}

2.FizzBuzz:
for(i=1;i<=100;i++){

if(i%15==0){document.write(“FizzBuzz
”)}

else if(i%5==0){document.write(“Fizz
”);}

else if(i%3==0){document.write(“Buzz
”);}

else {document.write(i+"
");}
}
3.Chessboard:
var hgt=8; var wdh=8; var a=" “; var b=”#"; var Print="";
for(var row=0;row<=hgt;row++){

for(var col=0;col<=wdh;col++)

{if((col+row)%2==0){Print+=a;} else{Print+=b;}
}
Print+= “\n” + “
” ; }

document.write(Print);

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

  2. 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 += “#”;}}board += “\n”;} console.log(board);

1 Like

Triangle Loop:
Solution 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);
  
}

Solution 2:

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

FizzBuzz Loop

Solution 1:

var count = 1;
var f = "Fizz";
var b = "Buzz";

  for (list = 1; count < 100; list++){
    if(list % 5 == 0 && list % 3 == 0)
    console.log(f+b)
    else if (list % 3 == 0) console.log(f);
    else if (list % 5 == 0) console.log(b);

      else console.log(count); count ++

Solution 2:

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

Chess Board

Solution 1:
var size = 8;
var board = “”;

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

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

Solution 2:

let size = 8;

let board = "";
    for (let r = 0; r < size; r++) {
    for (let c = 0; c < size; c++) {
          if ((c + r) % 2 == 0) { board += " "; }
          else { board += "#"; }
        } board += "\n";}
console.log(board);
1 Like

hey everyone, im having issues with this course. i feel that the things they are asking us to do were never taught. ie prompt or how to call these functions. i really thought these courses were going to be more teaching and follow along then heres a 4 min video try to things you were never taught. its becoming frustrating and deterring from continuing due to the lack of guidance.

FIZZBUZZ:
for(i=1; i<=100; i++){
if(i%15==0){
console.log(“Fizzbuzz”)
}
else if(i%3==0){
console.log(“fizz”);
}
else if(i%5==0){
console.log(“buzz”)
}
else{
console.log(i);
}
}

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

    <script>
    var hashtags = ""
    counter = 0;
    while(counter < 7){
      var hashtags = hashtags + "#"
      counter++
      document.write("<h1>" + hashtags + "</h1>");
    }
    </script>

FizzBuzz

    <script>
    counter = 0;
    var number1 = 0;
    while(counter<100){
    var Fizz = 0;
    var Buzz = 0;
    number1++
    if (number1 % 5 == 0){
    var Buzz = 1
    }
    if (number1 % 3 == 0){
    var Fizz = 1
    }
    if (Fizz == 1 && Buzz == 1){
    document.write("<h1>FizzBuzz<h1>")
    } else if (Buzz == 1){
    document.write("<h1>Buzz<h1>")
    } else if (Fizz == 1){
    document.write("<h1>Fizz<h1>")
  } else {document.write("<h1>" + number1 + "<h1>")}
    counter++
    var Fizz = 0;
    var Buzz = 0;
    }
    </script>

Chessboard

    <script>
var width = parseInt(prompt("How wide do you want the chessboard to be?"))
var length = parseInt(prompt("How long do you want the chessboard to be?"))
var length = length / 2

var line1 = ""
var line2 = ""

while(width > 0){
var line1 = line1 + " #"
var line2 = line2 + "# "
width--
}

while(length > 0){
console.log(line1)
console.log(line2)
length--
}
    </script>
1 Like

Triangle Loop

var numbr_rows = 7;
for(var row = 0; row < numbr_rows; row++){
var print = “#”;

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

FizzBuzz

for (var i=1; i < 101; i++){
if (i % 15 == 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 size = 8;
var board = “”;
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
if ((x + y) % 2 == 0)
board += " ";
else
board += “#”;
}
board += “
”;
}
document.write(board);

1 Like

Even though this was very tough for me as this is the very first time I have ever been able to use code in any way form or fashion I still think that the exercises were awesome, the book was and is incredible to learn from and to practice with if you are as green and new as I am. Thank you so much Ivan on Tech Academy.

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

2.3 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

I definitely had to look at the answer to figure it out! But still great practise.

1 Like
  1. Looping triangle
  var numRows = 7, row = "";
      for(var i = 0; i < numRows; i++){
        row += '#';
        console.log(row);
      }
  1. FizzBuzz
      for(var i = 1; i <= 100; i++){
        var toPrint = i;
        if(i % 3 == 0 && i % 5 == 0) toPrint = "FizzBuzz";
        else if(i % 3 == 0) toPrint = "Fizz";
        else if(i % 5 == 0) toPrint = "Buzz";
        console.log(toPrint);
      }
  1. Chessboard
  var numRows = prompt("Enter the number of rows:"), 
  numCols = prompt("Enter the number of colomns:"), 
  row = "";
  for(var i = 0; i < numCols; i++) row += "# ";
  for(var i = 0; i < numRows; i++){
    console.log(i % 2 != 0 ? row : row.split('').reverse().join(''));
  }
1 Like

So true, totally agree with your learning lessons number 1 and number 2. In my case, I came to the realization I’ll have to go over many exercises in order to get this programming right. The solutions were much simpler than I thought. Oh well, I’m not giving up. Thank you for sharing your work and thoughts.

1 Like

For the FizzBuzz one the code I came up with was drastically more complex than the answer when I checked it. Not sure if anyone else ended up with something like this. They both work but I guess I need to learn to simplify.

This is mine.
Screen Shot 2021-05-04 at 4.31.18 PM

This is the actual answer.
Screen Shot 2021-05-04 at 4.34.05 PM

The checker board one I was unable to figure out on my own and still don’t quite get it even after looking at the answer.

1 Like

Looping a triangle

let num = 7;
let hash = "";

for(i=1; i<=num; i++){
  hash = hash + "#";
  console.log(hash);
}

FizzBuzz

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

Chessboard

let num = 8;
let board = "";

for(x=1; x<=num; x++){
  for(y=1; y<=num; y++){
    if((x+y)%2 == 0){
      board += " "
    } else
      board += "#"
    }
   board += "\n";
 }
 console.log(board)
1 Like

Yes, that’s the spirit! I hope by the end of your learning progress, you’ll look back and wonder how far you’ve come. Keep up the great work! :muscle:

Happy Learning!

Yes, Malik. It is not easy, this is all very new to me. However, I’m fascinated with what I have being learning here. I’m moving forward even if that means I have to do the same exercise a 100 times until I get it.
Thank you for your support,
Maia

2 Likes

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

2
for (var i = 1; i <= 100; i++) {
var output = “”;

if( i % 3 ==0) { output += “Fizz”; }
if( i % 5 ==0) { output += “Buzz”; }

if(output == “”) { output = i; }

console.log(output);

}

3

wip

2 Likes

Definitely getting more challenging. I had to look at answers here after I couldnt crack the code for a while. Ivan makes it look so much easier in his video but good learning experience

1 Like

It took me a couple of days to get this one right using everything I had learned so far. Here is the completed Javascript code. The result is displayed in the console.

// PRINT SQURE BOARD FROM 4 x 4 - TO 20 x 20
let board ="";
//THIS STRING VARIABLE ADDS A SPACE AND THE HASH AT THE SAME TIME
let squares =" #";
var w;
// WAIT FOR AN EVEN NUMBER BETWEEN 2 & 20 TO BE ENTERED
do { w = Number(prompt(“Enter a an even number from 4 to 20”))

} while (w < 3 || w > 20 || w%2 !=0);
// THIS LOOP CONCOTENATES ENTERED NUMBER OF SQARES TOGETHER
// I divided w by 2 to get the correct board size because the
// squares variable concatenates two squares at at time to the end
// board variable otherwise the board will be twice as big as w.
for (let count = 0; count < ((w/2) * (w/2)); count++) {
// THIWS LOOP ADDS A THE \n AFTER WIDTH(W) AMOUNT OF SQUARES

for (let count2 = 0; count2 < (w/2); count2++) {           

    if (count == ((w/2) * count2) && count > 0) {             

    board = board + "\n";
    }
} 
board = board + squares; 

}
console.log(board);

2 Likes

Yes it was very tough. I took me a few days. I kept on trying different things and then each day I woke up fresh, a new solution kept cane through to me. I learned to program in BASIC back in the early days of computing, so I kinda still got the logic of it.

1 Like

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 + "<br>");
        }

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";
  }
1 Like