Chapter 2 Exercises

Triangle problem:
var counter = 0;
var string = “#”;
while (counter < 7) {
console.log(string)
string = string + “#”;
counter++
}
Any suggestions as to whether this is an appropriate way to solve this problem?

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

Chess Board Problem:
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

I understand the logic, the computer is printing horizontal lines in a loop, if and else determine even/odd number, when it reaches end of loop, new line and starts the loop again, continues until value of size is 8. However, when I look at the code, it is not obvious to me. Can anyone look at my comments imbedded in the code to comment. Thanks.

var size = 8;

var board = "";

for (var y = 0; y < size; y++) {
  for (var x = 0; x < size; x++) {
    //how does the computer know to do horizontal line and not a vertical list?
    if ((x + y) % 2 == 0)
    //I understand the concept here, but don't all x and y values add up to even numbers? i.e. 1+1; 2+2; 3+3; etc
      board += " ";
    else
      board += "#";
      //is this the horizonal line loop block?
      
  }
  board += "\n";
  //a new line here and starting horizontal loop again?
}

Thanks for those links. I’m having a hard time with the code, but I do understand the logic on how to execute, I’m getting stuck on the language/coding part.

Challenging for sure. Luckily I love a challenge.

i’m pretty new to all this as well but it is my understanding that the x loop is inside the y loop and the x loop needs to run its whole course (multiple iterations) before the y loop can start the next iteration. The x loop is the horizontal line and it will go all the way until it is no longer less than the size. Then the Y loop can move on to the second iteration or the second line

the chessboard doesn’t look like the example. But a very simple way to approach. I spent several hours and had to google the results to help me out

FizzBuzz
Okay, so I first wrote this:
for (let i = 1; i < 101; i++) {
console.log(i);
if (i % 3 == 0)
console.log(“Fizz”);
if (i % 5 == 0)
console.log(“Buzz”);
if (i % 3 == 0 && i % 5 == 0)
console.log(“FizzBuzz”);
}
and quickly realized that I was close, but my output was still printing numbers divisible by 3 and 5, so I adjusted the code to this:
for (let i = 1; i < 101; i++) {
console.log(i);
if (i % 3 == 0)
console.log(“Fizz”);
if (i % 5 == 0)
console.log(“Buzz”);
if (i % 3 == 0 && i % 5 == 0)
console.log(“FizzBuzz”);
}
and realized that only printed Fizz and Buzz words, but not the numbers. After 45 minutes after I started (LOL), I realized I needed to incorporate else statements to obtain the correct output and came up with this;
for(i = 1; i <101; i++) {
if (i % 3 == 0) {
console.log(“Fizz”);
}
else if (i % 5 == 0) {
console.log(“Buzz”);
}
else if ((i % 3 == 0) && (i % 5 == 0)) {
console.log(“FizzBuzz”);
}
else
{console.log(i);
}
}
and it worked! Nice lessons learned with this exercise!

Chess Board - after another hour+, I finally got this to work:

board_size = 8;
for(let row = 1; row <= board_size; row ++){
let board = “”;
for(let column = 1; column <= board_size; column ++) {
if ( (row + column) % 2 == 0) {
board += “#”;
}
else{board += " ";
}
}
console.log(board);
}

I did have indentations in my code as best as I could, but didn’t translate here. Anyway, I’ll never be a programmer at this speed!!! LOL

1 Like
  1. Looping a Triangle

var a = “”;
for (var i=0; i<7; i++){
a += “#”;
console.log(a);
}

  1. FizzBuzz

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

  1. ChessBoard (8x8)

var grid = 8;
var chessboard = “”;

for (var i=0; i<grid; i++){
for (var j=0; j<grid; j++){
if((i+j)%2==0){
chessboard += " "
} else {
chessboard += “#”;
}
}
chessboard += “\n”;
}
console.log(chessboard);

2 Likes

Looping a triangle:
let count = 0;
let hash = “”;
for(count;count<10;count++){console.log(hash = hash + “#”)}

FizzBuzz loop:

for( count = 1; count <=100; count ++){if(((count/5)%1) == 0 && ((count/3)%1) == 0){console.log(“BuzzFizz”)}else {if(((count/3)%1) == 0){ console.log(“Fizz”)}else {if(((count/5)%1) == 0){ console.log(“Buzz”)}else {(console.log(count))}}}}

Is this correct? it does do what the excersice says.
Also, it looks very messy. and i know that it is bad if it is messy. How do you clean it up?
I dont want to look at the answer just yet.
Thanks.

FIZZBUZZ

Exercises ch 2

  1. Fizzbuzz solution:

var max = 101;

for(var number = 1; number < max; number++){
if(number%15 ==0){
console.log(“FizzBuzz”);
}else if(number%3==0){

console.log(“Fizz”);
}else if(number%5 == 0){

 console.log("Buzz");

}else

console.log(number);

}

  1. Checker board solution

var size = 8;
var space = “”;

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

1 Like
  1. Looping Triangle.

    for (var i = "#"; i.length <= 7; i = i + "#") {
          console.log(i);
    }
    
  2. FizzBuzz

     for (var i = 0; i < 100; 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");
           }
    
           console.log(i);
        }
    
  3. Chessboard

    var a = " ";
    var b = "";
    var dim = 8
    
       for (var coluna = 0; coluna < dim/2; coluna++) {
     
    
              a += "# ";
              b += "# ";
         }
    
         console.log(a + "\n" + b);
    
         var c = a + "\n" + b;
    
             for (var linha = 1; linha < dim/2; linha++ ) {
    
                    c = c + " ";
    
                    console.log(c);
             }
1 Like

Triangle Exercise

  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

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

ChessBoard Exercise

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

This is my solution for exercise number 1.
I tried to keep it simple.
What do you think?

let figure = " “;
for (counter = 0; counter<7; counter++){
console.log(figure+=”#");
}

1 Like
  1. Looping a triangle
    for ( var row="#"; row.length<8; row+="#"){
    console.log(row);
    }

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

  3. Chessboard
    var size=8;
    var board = “”;

for(var a=0; a<size; a++){
for (var b=0; b<size; b++) {
if((a+b) % 2 === 0) {board += " ";}
else {board += “#”;}
}

board += “\n”;}
console.log(board);

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

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

  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

FizzBuzz
for (number = 1; number <= 100; number++){
console.log(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”);
}
}

2 Likes

TRIANGLE LOOP

y="#"
for(x=0;x<=6;x++){
console.log(y);y=y+"#"
}

FIZZBUZZ EXERCISE

for(x=0;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 BOARD

b=" "

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

1 Like

Triangle problem pasted but does not show. Not sure why, please view edit to see code. Thanks

Loops!

Hopefully my loops work

1 Like

Chessboard

var board = " ";
var chess = 8;

for (var row = 0; row < chess; row ++){
for (var column = 0; column < chess; column ++){
if ((row + column) % 2 === 0)
board += “#”;
else
board += " ";
}
board += “\n”;
}
console.log (board);

Fizz Buzz
var print = " ";
if (i=1; i<= 100; i ++)
if (i % 3 === 0 && i % 5 === 0)
print += fizzbuzz;
else if (i % 3 === 0)
print += fizz;
else if (i % 5 === 0)
print += buzz;
else (i % 3 != 0 && i % 5 != 0)
console.log (i);

Hi Nazia, Please do fix the syntax in the code. You appear to be missing the if-else structure in your answer. Also, remember to post your answers in the preformatted format. Simply highlight your code and click on the “preformatted text” icon in your input box.