Chapter 2 Exercises

Fizz Buzz

 num_count = 0;
  for(let counter = 1; counter<101; counter++){
    num_count = num_count + 1;
    if ((num_count % 3 === 0) && (num_count % 5 === 0)) {
      console.log("FizzBuzz");
    }

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

    else {
      console.log(num_count);
    }
    
}

Chessboard
I had an insanely difficult time figuring this one out but found Thecil’s explanation. It’s a lot easier after you see the solution haha.

  var size = 8;
      var 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
let row = "#"
while (row.length < 8){
    row += "#"
console.log(row)
}
  1. Thanks to Guatoshi for the helpful info on this one!
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);}
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 += "\n";
}
console.log(board);
1 Like

TRIANGLE EXERCISE

            var rows = 7;

            for(var i = 0; i < rows; i++) {

                var toPrint = "#";

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

                    toPrint += "#";

                    

                }

                console.log(toPrint);

            }

FIZZBUZZ EXERCISE

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

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

                    var fourPrint = "FizzBuzz";

                    console.log(fourPrint);

                }

                else if((i+1) % 3 == 0) {

                    var toPrint = "Fizz";

                    console.log(toPrint);

                }

                else if((i+1) % 5 == 0) {

                    var threePrint = "Buzz";

                    console.log(threePrint);

                }

                else {

                    console.log(i+1);

                } 

            }

CHESSBOARD EXERCISE

var size = 8

            var chessboard = "";

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

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

                    if((i+j) % 2 == 0) {

                        chessboard += " ";

                    }

                    else {

                        chessboard += "#";

                    }

                }

                chessboard += "\n";

            }

            console.log(chessboard);
1 Like
1.

  for(let i=0;i<7;i++){
    for (let j = 0; j < i+1; j++) {
      document.write("#");
    }
    document.write("<br>");
  }
2.

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

3.

  var a;
  var n = 8;
  for (var i = 0;i<n; i++) {
    if(i%2 == 0){
      a = " #";
    }else {
      a = "# ";
    }
      for(var j=0;j<n;j+=n/2){
        a += a;
      }
      console.log(a);
  }
1 Like

thanks found your explanations very helpful should be unstuck soon. lol

found these exercises quite hard used a forum members explanations of his code and coding helped me clear it up somewhat his simplified coding broke it down for me.
still along way from it being easy for me but found there wording alot easier to understand than the javascript books definitions.

  1. looping triangle

for (var line = “#”; line.length < 8; line += “#”) console.log(line);
2.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);}
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

Where can I find the exercise, couldn’t find them in my book copy (Marijn Haverbeke - Eloquent JavaScript_ A Modern Introduction to Programming-No Starch Press (2011).pdf)

Thank’s in advance

Looping a Triangle
for(let triangle = “#”; triangle.length <= 7; triangle += “#”)
console.log(triangle)

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 spaceHashtag = " #";
let hashtagSpace = "# ";

for(let i = 0; i < 8; i++) {

if(i % 2 === 0) {
console.log(spaceHashtag.repeat(4));
} else {
console.log(hashtagSpace.repeat(4));
}
}

//With a little ( a lot of) help from a YouTube channel called iVuDang. He has lengthy videos that go in to detail for each excercise.

2 Likes

hello, im having a hard time thinking about how do I do it myself without copy pasting from anyone else, how do I learn that?

Hey @josejalapeno, hope you are great!

The way I could advice you is to try to code without any other help rather than google and yourself, the forum or exercise result should be only to compare your result against others, not that yours is incorrect, just to learn and improve.

You have all the video lessons before the exercises, also you could use google to ask about any question, you can also use this website to learn methods and syntax use cases https://w3schools.com/

Carlos Z

1 Like

Triangle:

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

FizzBuzz:

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

Chess Board:

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 += “\n”;
}

console.log(board);

1 Like

Looping Triangle
for(var i = 0; i < 7; i++)
{
var toprint= “#”;
for(var j = 0; j < i ; j++)
{
toprint = toprint + “#”;
}
console.log(toprint);
}

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

Chess Board
let spaceHashtag = " #";
let hashtagSpace = "# ";
for(let i=0; i<8; i++){
if(i%2 === 0){
console.log(spaceHashtag.repeat(4));
} else {
console.log(hashtagSpace.repeat(4));
}
}

1 Like

FizzBuzz

var number = 101;
for (var row = 1; row < number; row++){
var toprint = row;

if (row % 3 == 0){
toprint = “Fizz”;
}

if ((row % 5 == 0) &&! (row % 3 == 0)) {
toprint = “Buzz”;
}

if ((row % 5 == 0) && (row % 3 == 0)) {
toprint = “FizzBuzz”;
}

for (var column = 0; column<row; column++){
toprint + " ";

}
console.log(toprint);
}

ChessBoard

var number = 8;
for (var row = 0; row < number; row++){
var toprint = " # # # #";

if (row %2 == 0) {
toprint = “# # # #”;

for (var column = 0; column<row; column++){
toprint += " "
}

}
console.log(toprint);
}

1 Like

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

console.log(board);

1 Like

As I observe the discussion, there are multiple ways to go about completing the exercises, but the principles are the same. I think for me find a simplistic way to express my Javascript code is going to help not only understand Javascript, but also excel in it.

When I see this:

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

It is basically saying to have 100 numbers, and for every number divisible by 3 or 5 to have the output say Fizz or Buzz respectively. Then of course, you can have the output for numbers divisible by 3 or 5 be FizzBuzz. After taking time to look the code from this perspective, I’m able to start understanding Javascript a little better.

Fizzbuzz - learned a lot just by comparing my answer to the textbook answer, could have made my code much shorter and succinct

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

            if (count == 0) {

                continue;

            }

            if (count % 3 == 0) {

                console.log("fizz. count is " + count);

            }

            if (count % 5 == 0) {

                console.log("buzz. count is " + count);

            }

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

                console.log("fizzbuzz. count is " + count);

            }

        }

Chessboard - I realise I kind of did this one wrong, I printed a new line each time whereas the textbook uses /n to create a new line and logs the answer only once

var num_rows = 8;

        var hashCount = 8;

        var printPatternEven = " #"

        var printPatternOdd = "# "

        var multiStr = ""

        for (var row = 0; row < num_rows; row++) {

            if (row % 2 == 0 || row == 0) {

                // console.log(row)

                for (var hash = 0; hash < hashCount; hash++) {

                    multiStr += printPatternEven

                }

                console.log(multiStr)

                multiStr = ""

            }

            if (row % 2 == 1) {

                // console.log(row)

                for (var hash = 0; hash < hashCount; hash++) {

                    multiStr += printPatternOdd

                }

                console.log(multiStr)

                multiStr = ""

            }

        }
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 += “#”
// }
// console.log(toPrint);
// }
//Fizz Buzz
// for (let number = 1; number <= 100; number ++) {
// if (number % 3 == 0) console.log(“Fizz”);
// else if (number % 5 == 0) console.log(“Buzz”);
// else console.log(number);
// }
// Chessboard
// for (row = 1; row <= 8; row ++) {
// if (row % 2 == 0) console.log("# # # # “)
// else console.log(” # # # #")
// }

1 Like

Trying to understand the logic of the Chessboard solution. So whenever we introduce two numeric variables (x & y, a & b, etc.), the program interprets/treats them as coordinates on two axis?

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

FizzBuzz

var num_rows = 100;
for(var row = 1; row < num_rows; 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(row);

Checker Board

var size = 8;
var cboard = “”;

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

2 Likes