Chapter 2 Exercises

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(var num = 0; num<100; num++){
var toPrint = num;

if (num % 3 == 0){
  var toPrint = "Fizz";
}
if (num % 5 == 0){
  var toPrint = "Buzz";
}
if (num % 3 == 0 && num % 5 == 0){
  var toPrint = "FizzBuzz";
}
console.log(toPrint);
}

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

2 Likes
  1. The first Stars Exercise was very fun to consider how these exercises can be done in more than one way. While Ivan shows how to do it using two loops, one for columns another for rows, it can be done simpler with a for loop.

  2. The FizzBuzz Exercise was seemingly simple at first was became a challenge when trying to figure out how to log either number or Text. It was easy to figure out how to log both, this exercise was a good lesson in how to use the || operator and how to think in terms of if statements.

  3. This Exercise was challenging at first to get my mind around the loop within the loop and how they interact with each other. I spent lots of time researching and trying different things, originally I found it was easy to grasp using while loops then translating into the simper for loop.

These exercises were fun and admittedly challenging.

1 Like

for (let i = 1; i < 101; i++) {
if ((i%3 == 0) && (i%5 == 0)) { document.write(“

FizzBuzz

”);}
else if (i%3 == 0) { document.write(“

Fizz

”);}
else if (i%5 == 0) { document.write(“

Buz

”);}
else { document.write(<p>${i}</p>);}
}
let bindingSize = 4;
let loopSize = bindingSize / 2;
let row = "";
for(let i = 0; i < loopSize; i++) {
  row += "# ";
}

for(let i = 0; i < loopSize; i++) {
  console.log(" " + row);
  console.log(row);
}
1 Like

Thanks to you i was able to make things work thank you !

  • Fizz/Buzz:
        for(i=1;i<101;i++){
          if(i%3==0){
            console.log("Fizz");
          }
          else if(i%5==0){
            console.log("Buzz");
          }
          else{
            console.log(i);
          }
        }

Result: image

  • Chessboard:
        width = 6;
        height = 6;
        white = " ";
        black = "#";
        counter = 0;
        while(counter<width){
          white += "# ";
          black += " #";
          counter++;
        }
        for(counter=0;counter<(height/2);counter++){
          console.log(white);
          console.log(black);
        }

Result:
image

1 Like
  1. Looping a triangle

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

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

  1. 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

For both questions it was bit of a challenge but I was able to figure it out.
For the fizz buzz this was the code that did the trick for me:


for(let number=0;number<=100;number++)
{
if(number%3==0&&number%5!==0){console.log("Fizz");}
if(number%5==0&&number%3!==0){console.log("Buzz");}
if(number%3==0&&number%5==0){console.log("FizzBuzz")}
if(number%3!==0&&number%5!==0){console.log(number)}
}

For the chess board problem the following code did the trick for me:
let rows=0; for(let size=8;rows<size;rows++) { let width="# ".repeat(size*.5); if(rows%2==0){console.log(" "+width);} if(rows%2!==0){console.log(width);} }

1 Like

FizzBuzz

for(var number=1; number<=100;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 Like

Triangle
var rows=7;

for(a=1; a<=rows; a++) {

for(b=1; b<=a; b++) {

document.write(" # “);
}
document.write(”
");
}

FizzBuzz
for (i = 1; i < 101; i++){

if (i % 15 == 0)

    console.log("fizzbizz");

else if (i % 3 == 0)

console.log("fizz");

else if (i % 5 == 0)

console.log("bizz");

else console.log(i);}

Chessboard

var a = " ";

var b = “#”;

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

if (i % 2==0)

console.log(a+b+a+b+a+b+a+b);

else console.log(b+a+b+a+b+a+b+a);}
1 Like
Triangle Loop:

  var num_rows = 7;
        for(var row = 0; row < num_rows; row++){
          var toPrint = "#";
          for(var column =0; column<row; column++){
            toPrint += "#"
          }
          document.write(toPrint + "<br>");
        }

FizzBuzz Loop:

 for (var i=1; i < 10; i++){
        if (i % 15 == 0) document.write("FizzBuzz <br>");
          else if (i % 3 == 0) document.write("Fizz <br>");
            else if (i % 5 == 0) document.write("Buzz <br>");
              else document.write(i + "<br>");
}


ChessBoard Loop:

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 += "&nbsp;";
      else
        board += "#";
    }
      board += "<br>";
}
document.write(board);
1 Like

// write exercise code

var num_rows = 7;
for(var row = 0; row < num_rows; row++){
var toPrint = “#”;
for(var column =0; column<row; column++){
toPrint += “#”;
}
document.write(toPrint + “
”);
}

1 Like

Fizzbuzz

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

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
**Looping a triangle:**

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

**Fizz Buzz**

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

**Chess board** Needed a little of research and a hint on this one 

let rows =prompt("insert number of lines");
let lines=prompt ("insert njumber of columns");
let board = "";

for (let y = 0; y < rows; y++) {
  for (let x = 0; x < lines; x++) {
    if ((x + y) % 2 == 0) {
      board += " ";
    } else {
      board += "#";
    }
  }
  board += "\n";
}

console.log(board);
1 Like
           **FiIZZBUZZ solution**                                                                                                                                  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 solution***                                                                                                                            
  var size = 8;
  var board = "";
  for (var y = 0;  col < size; y++) {
    for (var x = 0; row < size; x++) {
      if ((x+y) % 2 == 0) {board += " ";
      }
      else {board += "#";
      }
    }
    board += "\n";
  }
  console.log(board);
1 Like

FizzBuzz

for (var i=1;i<=100;i++){
text1="";
if (i%3==0) {text1=text1+“fiz”;};
if (i%5==0) {text1=text1+“buz”;};
if (i%3!=0&&i%5!=0) {text1=i;};
console.log(text1);
}

**Chessboard**
size=8;
for (var i=1;i<=size;i++){
text="";
for(var j=1;j<=size;j++)
{
if((i+j)%2==0){text=text+" “} else {text=text+”#"};
};
console.log(text);
}
1 Like

I hope my brain on fire is not a good sign of what learning all the rest of this is like. I finally had to just reread much of the chapter and then still check their solution and play around with a few changes to understand what each part was really doing. However I learn, I learn, I suppose…

  1. Looping a Triangle

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

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

  2. 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

Triangle Loop
for(var a="#";a.length<8;a+="#")
console.log(a)

FizzBuzz
for(i=1;i<=100;i++){
var output=""

if(i%3==0){output+=“Fizz”;}
if(i%5==0){output+=“Buzz”;}

if(output==0){output=i;}
console.log(output)
}

ChessBoard Loop

var size=8;
var result= “”;
var row = 1;

while( row<=size){
var column = 1

while(column<=size){

if((column + row)%2===0){

result += " ";
} else {
result+= “#”
}

column += 1;

}
result +=’\n’;
row+=1;
}

console.log(result)

1 Like

Triangle Loop
var num_rows = 7;
for(var row = 0; row < num_rows; row++){
var toPrint = “#”;
for(var column =0; column<row; column++){
toPrint += “#”;
}
document.write(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
1. Triangle Exercise:
for(let line = "#"; line.length<8; line+="#") {
console.log(line);
}
2.  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);
}
3. Chessboard
let size = 8;
let board ="";
for(x=0; x<8; x++) {
for(y=0; y<8; y++){
if((x+y)%2==0) {
board += " ";
} else {
board +="#";
}
}
board += '\n';
}
console.log(board);
1 Like
Exercise01 Triangle
<script>

for (var row = 1; row <= 7; row++){
    var output = "";
    for (var column= 1; column <= row; column++){
        output += "#";
    }
    console.log(output, "\n");
}

</script>
Exercise02 FizzBuzz
<script>

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

</script>
Exercise03 Chess Board
<script>

var output = "";

for (var row = 1; row <= 8; row++){

    var indent = 0  
    if((row % 2) == 0) indent = 1
    for (var col = 1; col <= 8; col++){
        if(((col + indent) % 2) == 0){
            output += "#";
        }
        else  output += " ";
    }
    output += "\n";
}
console.log(output);

</script>
1 Like