Chapter 2 Exercises

For the triangle exercise I put the Print variable as the state variable to see what would happen and got this.

image

Can someone explain why it is incorrect? Thanks!

I think you need to empty the Print variable to a blank like “” once the innermost loop completes before it starts the outerloop again.

So try adding Print=“”; to the main loop ie. after the document.write statement.

Else it will simply add more hashes onto the Print variable which already has hashes stored in it from the prior run.

Hey @Meddodreddo do you have a discord by any chance? I’d love to have a study partner if you want to do that.

As for the triangle exercise, I’m still not quite understanding. If Print ="#" here:
image
Why doesn’t it add more hashes onto the print variable which already has hashes stored on it?

Unfortunately I don’t have a discord yet, but will look into that.
Try moving var Print=“#” to after your document.write statement and reword it as Print=“”;

Additionally, and this may just be only my style, define all the variables before using them for or in your code. Like var arow= , var Print= , etc.

Oh an refresh the webpage then that you’re using for the console, then try the code again if the logic makes sense to you, so as to prevent any issues.

LOOPING_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_LOOP
for (var a = 1; a <= 100; a++) {
if (a % 3 === 0 & a % 5 === 0) {
console.log(a + " FizzBuzz");
}
else if (a % 3 === 0) {
console.log(a + " Fizz");
}
else if (a % 5 === 0) {
console.log(a + " Buzz");
}
else {
console.log(a);
}

CHESSBOARD_LOOP
var size = 8;
var block = ‘#’;
var space = ’ ';

function chessBoard(ChessSize){
for (var i = 0; i < ChessSize; i++) {
var drawLine = ‘’;
for (var y = 0; y < ChessSize; y++){
if (i%2) {
if (y%2) {
drawLine = drawLine + space;
} else {
drawLine = drawLine + block;
}
} else {
if (y%2) {
drawLine = drawLine + block;
} else {
drawLine = drawLine + space;
}
}
}
console.log(drawLine);
}
}
chessBoard(size);

Checkerboard:

     var num_rows = 0;
     row1 = "# # # #";
     row2 = " # # # #";
    
    while (num_rows <8){
     if(num_rows < 8){
      document.write("\n" + "# # # #" );
      num_rows ++;
  
    }
    if(num_rows < 8){
      document.write("\n" + " # # # #" );
      num_rows ++;
  
    }
  }

FizzBuzz:

    for(let a = 1; a <= 100; a++) { 

if(a % 3 == 0){
document.write("fizz");
}
else if(a % 5 == 0){
    document.write("buzz");
}
else if(a % 3 == 0 && a % 5 == 0){
    document.write("FizzBuzz");
}

document.write(a);


}

I have a few questions on fizz buzz because I was not able to solve it without searching it up. To my understanding the % sign’s function will divide and give the remainder of the given number, so if we divide 3 by 3 we get 1, and this is not equal to 0, if we divide 6 by 3 we get 2. So I think I might have the wrong understanding of the % symbol. also when I used \n, it did not start a new line. lastly the part where I wrote Fizz and Buzz together was not working even though I used && logic.

Thank you!

1 Like

Java script reads from the top down so running through the loop it already reads that a is divisible by three and never reaches to the last else if for fizz buzz. If you move the Fizzbuzz to the top it will work.

1 Like

Looping Triangle:

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

    var toPrint = "#"
    for (var column = 0; column <number; column++){
      toPrint += "#";

        }
          console.log (toPrint);
  }

Fizz Buzz:


    var number_rows = 100

    for (var number = 1; number <= number_rows; number++ ){

    if (number % 3 == 0 && number % 5 == 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 Board: Had trouble with this one :confused: Had to use more resources then I wanted.

var boardSize = 8;
 var squares = "";

  for (var row = 0; row < boardSize; row++) {
    for (var letter = 0; letter < boardSize; letter++)
      if ((row + letter) % 2 == 0){
        squares += " ";
      }
      else {
        squares += "#";
      }


      squares += "\n";

    }
      console.log(squares)
1 Like

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

CHEESSBOARD:

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

Took me a while… but eventually got there!

Looping a triangle

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

FizzBuzz

for (var count=1; count <= 100; count++){
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);
}

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

2 Likes

Good work! For future reference, use the Preformatted Text trigger to type/format your code. :slightly_smiling_face:

You can open this by clicking the </> button above the text box, or with ctrl+shift+c.
It makes your answers look wayyy better and it’s easier to read!

2 Likes

No worries. Keep up the persistence and you will get a hang of this very soon!

Happy Learning! :smiley:

Hello, (Please help if you can)

As you may see from the below code structure I am just learning to program now.

I am struggling with an issue, and I am wondering if anyone can help me understand what is going on.

Every time my code is supposed to print the FizzBuzz it also prints Fizz and Buzz seperately.

FizzBuzz

I can not figure out why my code is still processing if-else for the first if statement when it should only process the first part of the ef statement and then skip the else section?

var num_rows = 100;
var toPrint = 1;

for(var row = 0; row < num_rows; row++)
 {
   console.log("MainVarRowCount" + " " + row);
  RemainderValue5 = toPrint % 5;
  RemainderValue3 = toPrint % 3;
  if (RemainderValue5 == 0 && RemainderValue3 == 0)
  {
    console.log(" FizzBuzz" + " " + toPrint);
  //  console.log(" FizzBuzz");
  } else {
  RemainderValue3 = toPrint % 3;
  }
  if (RemainderValue3 == 0)
  {
  console.log(" Fizz" + " " + toPrint);
//  console.log(" Fizz");
  } else {
  RemainderValue5 = toPrint % 5;
  }
  if (RemainderValue5 == 0)
  {
    console.log(" Buzz" + " " + toPrint);
  //  console.log(" Buzz");
  } else {
    console.log(toPrint);
  }
          toPrint ++;
  }

Thank you.

unless I am mistaken, divide 3 by 3 is 1 but there is 0 left so the value you keep for this expression is 0, so if you divide 4 by 3, the answer is also 1 but you have 1 left, which would mean the expression will deliver 1 or if you divide 5 by 3 you get 1 but you have 2 left (because 5 - (1 x 3) = 2) so the expression will deliver 2, hope this helps. % is the remainder operator, so you end up with what is left after you have divided what you can (without resulting in fractions).

  1. `
for (let line = "#"; line.length < 8; line += "#")
  document.write(line + "<br>");
  1. I know that exercise says to use console.log but I couldn’t make it with it, it just didn’t appear anything when I tried to use console.log so that’s what I done:
for (var number=1; number <= 100; number+=1){
          if (number % 3 ==0 && number % 5 == 0 ) document.write("FizzBuzz <br>");
          else if (number % 3 == 0) document.write("Fizz <br>");
          else if (number % 5 == 0) document.write("Buzz <br>");
          else document.write(number + "<br>");
        } ;
  1. grr after three days, still not working for me, even the book solution doesn’t come up like it suppose to. can someone tell me why after I put <\n> in code below, it doesn’t make new line but something like this: # # # #< ># # # # < > # # # #< ># # # # < > # # # #< ># # # # < > # # # #< ># # # # < >
    ` ```
    let size = 8
    let line = " #"
    let bline = "# "
    for(x=0; x< size / 2; x++){document.write(line.repeat(size/2) + “<\n>” + “”+ bline.repeat(size/2) + “<\n>”) };

Looping Excerises

While

let triangle = “#”;
while (triangle.length <= 7) {
console.log(triangle);
triangle +="#";
}

For

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 exercise

Let f = 1;
While (f <= 100) {
if (f % 15 == 0) console.log(“FizzBuzz”);
else if (f % 5 == 0) console.log(“Buzz”);
else if (f % 3 == 0) console.log(“Fizz”);
else console.log(f);
f = f + 1;
}

Chessboard excercise

Let size = 4;
Let board = “”;

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

1 Like

On let hashtag = “#”;
while(hashtag.length<7>){
console.log(hashtag);
hashtag = hashtag + “#”;
}
console.log(hashtag);

for(var i=1; i<=100; i++) {
// If the number we are looking at (loop variable “i”) is divisible by 3 AND 5
// examples - 15, 30, 45,60, 75, 90

else if( i%3 == 0) {
console.log(“Fizz”);
}
// 3 * 33 = 99 is less than 100

else if( i%5 == 0) {
console.log(“Buzz”);
}
// 5 * 20 = 100

If( i%3 == 0 && i%5 == 0) {
console.log(“FizzBuzz”);
}
else {
console.log(i);

}
}

1 Like
  1. Looping a Triangle
var rows = 0;
var columns = "#";
while(rows < 7){
    for(counter = 0; counter < 7; counter++){
        console.log(columns);
        columns+="#";
        rows++;
    }
}
  1. FizzBuzz
var count = 0;
while(count <= 100){
    if(count == 0){
        count++;
    }else if(count%3 == 0 && count%5 != 0){
        console.log("Fizz");
        count++;
    }else if(count%3 != 0 && count%5 == 0){
        console.log("Buzz");
        count++;
    }else if(count%3 == 0 && count%5 == 0){
        console.log("FizzBuzz");
        count++;
    }else{
        console.log(count);
        count++;
    }
}
  1. Chessboard
var binding = 8;
var rowodd = "";
var roweven = "";
for(i=0;i<binding;i++){
    if(i%2==0){
        rowodd+=" ";
        roweven+="#";
    }else{
        rowodd+="#";
        roweven+=" ";
    }
}
for(i=0;i<binding;i++){
    if(i%2==0){
        console.log(rowodd);
    }else{
        console.log(roweven);
    }
}
1 Like