Chapter 2 Exercises


#269

Hi people, for the Fizzbuzz exersise I wrote

for (var b=0;b<=100; b++){

if(b%3==0) console.log("fizz");
else if (b%5==0 && b%3!=0) console.log("buzz");
else if (b%3==0 && b%5==0) console.log("fizzbuzz");
else (console.log(b));

}

Can someone explain to me why

else if (b%3==0 && b%5==0) console.log(“fizzbuzz”);

is not yielding a fizzbuzz in the console?

thanks,


#270

your third line should yield buzz only if the number is divisible by 5 not NOT 3 such as 5 and 25.

if (counter % 5 == 0) { document.write("

the counter is now BUZZ

"); }

#271

your Fizzbuzz code yields a

FizzBuzz
Fizz

for unumbers that are both divisible by 3 and 15.


#272

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

  else if(counter % 3 == 0)
  {console.log("Fizz")}

  else if(counter % 5 == 0)
  {console.log("Buzz")}

  else
  {console.log(counter)}
}

Chessboard (only partial):

var height = 8;
for(var counter = 0; counter < height; counter++)
{
if(counter % 2 == 0 && counter < height)
{console.log(" # # # #")}
else
{console.log("# # # # ")}
}


#273

Was fun doing things ourselves for once, nice stuff !

Triangle
for (var x = 0, line = “#”; x < 9; x++)
{
line = line + “#”
console.log(line);

}

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

  }

Chessboard
var gridsize = prompt(“number”)

for (var y = 0; y < gridsize; y++)
{
if(y%2==0)
{
for (var x = 0, row = " "; x < gridsize; x++ )
{
if (x%2==0){
row = row + “.”;
}
else {
row = row + " " ;
}
}
console.log(row);
}
else
{
for (var x = 0, row = " "; x < gridsize; x++ )
{
if (x%2==0){
row = row + " ";
}
else {
row = row + “.” ;
}

  }
  console.log(row);
}

}


#274

By the way guys, with Fizzbuzz, i tried to do it many ways the only working method seems to be matching the “FizzBuzz”'s first and then doing the fizzes and buzzes seperately, else the code will generate duplicates, where on like 15 you will see Fizzbuzz, Fizz,Buzz if you get what I mean :slight_smile:

This is actually for real an interview one, turns out this is one of the things my cousin passes to juniors during the interview process.


#275

Okay, that’s a cool thing :slight_smile:
From this level I am still a bit far away…


#276

I added some comments to the code to help you understand what is going on there.

One of the best things to have on hand is library or API reference for whatever you are working with:

Here’s one for jquery: https://www.w3schools.com/jquery/jquery_ref_overview.asp
And one for css: https://www.w3schools.com/cssref/default.asp

You don’t have to know everything thats in these, but if there is something that you want to accomplish you can consult the reference and typically find some ready made solution for what you want. :smiley:


#277

Thanks Nik

now corrected

<script>


    for(var counter = 1; counter<100; counter++){

            document.write("<H2>  Another bloody loop </H2>" );

            document.write("<h3> the counter is now " + counter + "</h3>" );

             if (counter % 3 == 0) { document.write("<h3> the counter is now FIZZ </h3>"); }

            if (counter % 5 == 0 && counter % 3 != 0) { document.write("<h3> the counter is now BUZZ </h3>"); }

            if (counter % 5 == 0 && counter % 3 == 0) { document.write("<h3> the counter is now FIZZBUZZ </h3>"); }

    }


</script>

#278

Exercise #2 and #3 solutions:

Click to show solutions
<!DOCTYPE html>
<html>
  <head>
    <title>Exercise!</title>
  </head>
  <body>
    <script type="text/javascript">
      function fizzbuzz() {
        console.log("Exercise 2: FizzBuzz");
        length = 100;
        for(let i = 1; i <= length; i++) {
          var result = "";
          if (i % 3 == 0) {
            result = "Fizz";
          }
          if (i % 5 == 0) {
            result += "Buzz";
          }
          result = result || i;
          console.log(result + "\n");
        }
      }
      function chessboard() {
        console.log("Exercise 3: Chessboard");
        for (let size = 1; size <= 12; size++) {
          console.log("Size = " + size);
          let result = "";
          for (let i = 0; i < size; i++) {
            for (let j = 0; j < size; j++) {
              result += (i + j) % 2 == 0 ? " " : "#";
            }
            result += "\n"
          }
          console.log(result);
        }
      }
      window.onload = function() { fizzbuzz(); chessboard(); };
    </script>
  </body>
</html>

#279

LOOPiNG TRIANGLE

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

FIZZ BUZZ

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

CHESS BOARD

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

#280

fizzbuzz:

for (num1 = 1; num1 < 100; num1++){

if (num1 % 3 == 0 && num1 % 5 == 0){
  console.log("FizzzzBuuuzzzz");
}

else if (num1 % 3 == 0){
  console.log("Fizz");
}

else if (num1 % 5 == 0){
  console.log("Buzz");
}

else {
  console.log(num1);
}

}

Chessboard:
var size = prompt(“enter number”)
for(var x = 0; x < size; x++){

  var board = "";

    for(var y = 0; y < size; y++){
        if ((y+x) % 2 == 0){
          board += " ";
        }
  else{board+="#"}
  }
  console.log(board)
  }

#281

took me days to figure this out. But it finally worked and I am proud of it!
the following video definitely helped https://www.youtube.com/watch?v=2mWn1ta8z4M

chessboard

var size = 8;
for (y=0; y<size; y+=1){
if (y%2==0){
for (x = “”; x.length < size; x+=" #") {}
}
else { for (x = “”; x.length < size; x+="# ") {}}
console.log(x); }


#282

Perhaps I could have found a solution with fewer lines of code, but this should be fairly straight forward. I think :stuck_out_tongue:

for (i = 0; i < 101; i++){

  if(i % 3 === 0 && i % 5 != 0){

    console.log("fizz");
  }

  if(i % 5 === 0 && i % 3 != 0){

    console.log("buzz");

    }

    if(i % 3 === 0 && i % 5 === 0){

      console.log("fizzbuzz");

      }

    if (i % 3 === 0 || i % 5 === 0){

      continue;

      }

  console.log(i);

  if(i === 101){
    break;
  }

}

#283

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

Chess:
var size = 8;
var board = “”;
for (var row = 0; row<size; row++){
for (var col = 0; col<size; col++){
if ((row + col) % 2==0){
board+=(" “);
}
else {
board+=(”#");
}
}
board+="\n";
}
console.log(board);

Pyramid:
var size = 10;
for (var line = “#”; line.length < size; line += “#”)
console.log(line);


#284

Triangle

var finalOutput = ""

for(var t=1; t < 8; t++) {
  finalOutput += "#";

 console.log(finalOutput)
  }

FuzzBizz

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

    //not very effecient, but it gets it done.
   
    var oddRow=true
    var finalOutput = ""
    
    for(var c=1; c < 9; c++) {
      for( r=1; r < 9; r++){

        if(oddRow){

          if(r % 2 == 0){
            finalOutput += "#";

          }else {
            finalOutput += " ";
            //
        }}
        else {
          if(r % 2 == 0){
            finalOutput += " ";

          }else {
            finalOutput += "#";
          }
        }
      }
      finalOutput += "\n"

      if(oddRow){
        oddRow = false;
      }else {
        oddRow= true;
      }
    }

        console.log(finalOutput)

#285

Hi @jaydenhawkes123, I think some of your code will fail. In fizzbuzz you aren’t catching the FizzBUZZ option (divisible by both 5 and 3). Just a friendly headsup. Good luck.


#286

That fizzbuzz double if-statement is sharp.

Minimal amount of conditional checking. Avoids checking for divisible by 5 if not needed. Better solution than an && option (what I did).

I think in solidity, this would save user gas.

:clap:


#287

Here is my code I used to finish the assignments. The Chessboard and its code is still a bit confusing for me. The amount of curly brackets and when they are to be used is confusing to me as a beginner.

  1. Looping Triangle
    // write exercise 2 code…looping a triangle (ivans way)
    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);
    }

    // looping a triangle (book example)
    for (let line = “#”; line.length < 8; line += “#”)
    console.log(line);

  2. Fizz Buzz
    // fizz buzz assignment answer
    for (let n = 1; n < 102; n++) {
    let output = “”;
    if (n % 3 == 0) output += “fizz”;
    if (n % 5 == 0) output += “buzz”;
    console.log(output || n);
    }

  3. Chessboard
    //chessboard (this one I need clarification on the code) curly brackets confusing

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


#288

Also my version of the code, probably terrible, but seems to work…

fizzbuzz

let count = 0;

while (count<=100) {

  if ((count%15) == 0) {
  console.log("fizzbuzz");
    }
  
  else if ((count%3) == 0) {
  console.log("fizz");
    }

    else if ((count%5) == 0) {
    console.log("buzz");
      }
  else {console.log(count);
    }
  
  count ++;
} 

CHESSBOARD

var test = 20;
let chess1=" #";
let chess2="# ";

for (var i=2; i <= test; i++) {
  chess1 =chess1 + " #";
  chess2 =chess2 + "# ";  } 

	if (i=test) {
    chess1 = chess1 + "\n";
    chess2 = chess2 + "\n";
    string = chess1 + chess2;
    for (var z=1; z<i; z++ ) {
     string = string + (chess1 + chess2);
    }
  }

console.log(string)