Chapter 2 Exercises

Triangle

for(var line= "#"; line.length < 8; line+="#")
console.log(line);

  • < 8 means that the lengt will not exeed 8, cant be more than 8 bits. line += means, “#” will be added each loop to the variable
  • give line the string varaiable “#”, the .lenght counts how many letters it is in the variable,*

FizzBuzz

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

If var a is diversible with 3 AND 5, logg FizzBuzz
else, if var a is diversible with 3, logg Fizz
else, if var a is diversible with 5, logg Buzz.
else, console log a (which is the numbers in the loop of 1 to 100)

ChessBoard

let size = 8; *//how many steps it can max go to the side and down*
let board = ""; *//an empty line that will output a space, # or a new line*

//*the two for loops underneath are embedded within eachother* 


for(let a = 0; a < size; a++) {   `// for loop the vertical row` 
  for(let b = 0; b < size; b++) {` //for loop the horisontal row`
    if ((a+b) % 2 == 0){ // when we set a+b  every second number divisible by 2 will execute the space character ""  the loop goes 1,space,3,space,5,space,7,space
      board +=  " ";

    } else {
      board += "#"; //if not divisivble with 2, print #
    }
    
  }
board += "\n"; //new line: board +="\n" is the same as board = board + "\n", attached to the for loop that contains the b
}
console.log(board); //logging the final form of the board variable
2 Likes

Triangle

var i = "#";
var x = 7;
do {
    console.log(i);
    i += "#";

}
while(i.length <=x)

FizzBuzz

for(var x = 1; x <=100; x++) {
    if(x % 3 == 0 && x % 5 == 0){
        console.log("FizzBuzz");
    }

    else if(x % 3 == 0) {
        console.log("FIZZ");
    } else if (x % 5 == 0) {

        console.log("Buzz");
    } else {
        console.log(x);
    }

}

Chessboard

var size = 8;
var t1 = " ";
var t2 = "#";
for (var x = 1; x <= size; x++) {
    var row1 = t2 + t1;
    var row2 = t1 + t2;
    if(x % 2 == 0){
        console.log(row1.repeat(size/2));

    } else {
        console.log(row2.repeat(size/2));
    }    
  
}
1 Like

Triangle

for(line = "#"; line.length < 8; line += "#")
    console.log(line);

FizzBuzz

  var num = 100
  for(count = 1; count<=num; count ++){

    if(count % 3 == 0 && count % 5 == 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

var size = 8;
var board = "";

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

Looping A Triangle

            // Looping a Triangle
            var hashSymbol = ""
            for (var i=0; i<7; i++) {
                hashSymbol = hashSymbol + "#";
                console.log(hashSymbol);
            }

FizzBuzz

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

ChessBoard
Wow struggled alot with this one tbh. Made it after about an hour.

            // ChessBoard

            var size = 8;
            var cell = "";

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

I like the comments. Great work - very thorough!

2 Likes

So here it is:
Triangle:

for(row=0; row<7; row++)

{

let toPrint = "#";

for(col=1; col<=row; col++)
{

toPrint += "#";

}

console.log(toPrint);

}

FizzBuzz

for(var x = 1; x <=100; x++)   {
    if(x % 3 == 0 && x % 5 == 0)      {
      console.log("FizzBuzz");
    }

    else if(x % 3 == 0)     {
        console.log("FIZZ");
    }      else if (x % 5 == 0)     {

        console.log("Buzz");
    } 
else     {
        console.log(x);
    }

}

Chess

var x = 8, y = ' ';

for(var w = 0; w < x; w++){
for(var z = 0; z < x; z++){
if((w+z) % 2 == 0){
y += " ";
} else {
y += '#';
}
}
y += '\n';
}
console.log(y);

I was unable to remember all answers so I read some of them on the posts above.

1 Like

Thanks so much! I tried to understand as much as possible :slight_smile:

1 Like

You’re doing a super job! The chessboard one was tricky no doubt :pinching_hand:

1 Like

Triangle Loop

 let sharp=""; i=7;        
 while(i>0){
        sharp+='#';
        document.write("<h2>"+sharp+"</h2>");
        i--;
 }

FizzBuzz Loop

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

ChessBoard Loop

let size = 8;
        let flip= true;
        let board = "";
        for(let i=0;i<size;i++){
            for(let j=0;j<size;j++){
                if(flip){board += " "}
                else{board += "#";}
                flip=!flip;
            }
            board +="\n";
            flip=!flip
        }
        console.log(board);
1 Like

This is my code for the FizzBuzz. But I have one question. Why does it double write every fourth Fizz? :

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

if (i % 5 == 0)
{
console.log(“Buzz”)
}

if (i % 3 == 0)
{
console.log(“Fizz”)
}
if (i % 15 == 0)
{
console.log(“FizzBuzz”)
}
}

my solutions

//CHESSBOARD EXERCISE
var size = 8;
grid = ‘’;
var pound = ‘#’;

//for loop to create size x size grid
for (var row = 1; row <= size; row++) {

//creating each row depending on if it’s an odd or even row
for (w= 1; w <= size/2; w++) {
if (row%2 == 0) grid += '# ‘;
else grid += ’ #’;
}
//completing row with line break
grid += ‘\n’;
}
console.log(grid);

//FIZZBUZZ EXERCISE

count = 100;
for (var i = 1; i <= count; i++) {

FizzTrue = (i%3 == 0); // true if divisible by 3
BuzzTrue = ((i%5 == 0) && !FizzTrue); // true if divisible by 3 and not 5
FizzBuzzTrue = FizzTrue && (i%5 == 0); // true if divisible by 3 and 5

//printing results
if (FizzBuzzTrue) console.log("FizzBuzz ");
else if (BuzzTrue) console.log("Buzz ");
else if (FizzTrue) console.log("Fizz ");
else console.log(i + " "); // print number if not divisible by 3 and not divisible by 5
}

2 Likes

My chessboard answers: But for some reason in my code I had to put less than ten before i got 8 rows

for (i = 2; i<10; i++){
if (i % 2 == 0 ){
console.log(" # # # #")
}
else {
console.log("# # # #")
}

}

FIZZBUZZ

  for (let x = 1; x <=100; x++) {
    let y = " ";
    if (x % 3 == 0) y += "Fizz ";
    if (x % 5 == 0) y += "Buzz ";
    console.log(x + y)
    }

CHESSBOARD

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

    console.log (print)
</script>
1 Like

Hi Community!

I thought these exercises were very difficult as I have NEVER touched any coding before. I needed help from YT and Google to get this done. Although it is logical it is in fact learning a new written language so understanding it and actually writing it is very different in a way if you get what I mean.

I have written the solutions again and again until I was able to write them without using any help because I don’t want to move on without really getting it. Anybody else share this experience?

// #Loop 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);

}

// Using str.length

var str = “#”;

while (str.length <= 7){

console.log(str);

str += “#”;
}

2 Likes

FizzBuzz:

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

Chessboard:

var size = 20;
      var board = "";

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

I’ve been hitting my head against the table trying to figure these out. Are you guys figuring this stuff out the week after you started learning how to program? I feel like an idiot.

1 Like

Yah man, I have been struggling with it too. I also don’t want to move on until I get it. Its all new to me.

1 Like

There are so many approaches to each problem it seems so it is a bit confusing but it makes sense when watching somebody else do it… they likely have heaps of experience and are done banging their heads against the wall already.

To learn a new language we need to practice a lot… :slight_smile:

1 Like

Hello!

I was able to understand the first one - Then it took me like 40 minutos to check a solution, because i wasnt doing the logic right, finally found a working match and got great part of it.

Stuck on 3 - Will finish it soon.

2.- //Second part
for (var num=1; num<101; num++) {
var checkforthree = num % 3;
var checkforfive = num % 5;

if ((checkforthree == 0) && (checkforfive == 0))
console.log(“FizzBuzz”);

else if (checkforthree == 0)
console.log(“Fizz”);

else if(checkforfive == 0)
console.log(“Buzz”);

else console.log(num);
}

1 Like

this is the code for the triagle loop but i dont know how to run it.
help guys
thank you

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