Chapter 2 Exercises


#165

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


#166

Triangle Loop

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

I did the exercises a bit to soon. At first i didn’t realize length could be used this way. So i used sign < “########”. Which worked but looks less professional. :slight_smile:

FizzBuzz Loop

for (let number = 1; number <= 100; number++) {
if (number % 15 == 0) {
console.log(“fizzbuzz”);
} else if (number % 5 == 0) {
console.log(“buzz”);
} else if (number % 3 == 0) {
console.log(“fizz”);
} else {
console.log(number);
}
}

Again, i did this one too early and was quite pleased with myself to see it work. However scrolling down this page while posting this answer i saw the user Bitworld post a more professional tweak. Instead of:

if (number % 15 == 0)

He used:

else if (i % 3 && i % 5 == 0)

Chessboard Loop

let total = “”;
let size = “8”;

for (a = 0; a < size; a++) {
for (b = 0; b < size; b++) {
if ((a + b) % 2 == 0) {total += " ";}
else {total += “#”;}
}
total += “\n”;
}
console.log(total);

I needed help for this one, couldn’t figure out the logic behind it at first. But finally!


#167
for(var count=1; count<100; count++){
  if (count%3==0)
    document.write("<h2>Fizz</h2>")
  else if (count%5==0)
    document.write("<h2>Buzz</h2>")
  else
    document.write("<h2>" + count + "</h2>")
}

for(var count=1; count<9; count++){
if (count%2==0)
console.log(" # # # #")
else
console.log("# # # # ")
}


#168

FizzBuzz

for(i=1; i<=100; i++){
    !(i%5)?
    !(i%3)?
    console.log('FizzBuzz'):console.log('Buzz')
    :!(i%3)?console.log('Fizz'):console.log(i);
}
/* checks for divisibility by 5, then by three in the both cases of true and false */

ChessBoard

size = 8;
output='';

for(i=0; i<size; i++){
    for(j=0; j<size; j+=2){
        !(i%2)?output+=' #':output+='# ';
    }
    output+='\n';
}
console.log(output);
/* 
this solution may be looked at in a binary manner, where the outputs are 
either # and a space, or a space and #
of course, checking each position for a space or a # is possible, though this
method requires less iterations, since we increment by two to iterate each line
for half of the checkerboard size
*/

#169

FizzBuzz

let counter = 100;

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

Checks if the number is divisible by 3, then by 5, then by both. If neither, console.logs the number.

ChessBoard

let size = 8;
let total = "";

for(let row = 0; row < size; row++){
  for(let column = 0; column < size; column++){
    if((row+column)%2 == 0){
      total+=" ";
    }else{
      total+="#";
    }
  }
  total+="\n";
}
console.log(total);

Defines the number of rows; then in each row, it defines which character each column will have; then adds to the row a new line, and continues doing this for the number of rows defined


#170

Wow, short and easy to read.

My chessboard needs more code:

    let toPrint = "";
    let flipflop = true;
    for(let line = 1; line<=grid; line++)
    {
      if(line%2){flipflop=true;}
      else{flipflop=false;}
      for(let collumn = 1; collumn <= grid; collumn++)
      {
          if(flipflop){
            toPrint=toPrint+" ";
            flipflop=false;
          }
          else{
            toPrint=toPrint+"#";
            flipflop = true
          }
      }
      toPrint = toPrint+"\n";
    }
    console.log(toPrint);

What is the trick to get the code that simple?


#171

hell of a ride for me…
Triangle:

for (let i= “#”; i.length<=7; i+="#")
document.write(i + “
”);

fizzbuzz:

for (let i=1; i<=100; i++) {
if (i%3==0) {document.write(“fizz”);}

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

chessboard:

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


#172

Lopping a triangle:

for(let i="#"; i.length<8; i+="#"){
  console.log(i);
}

FizzBuzz

let n = 1;
while(n < 101){
  if(n % 3 == 0 && n % 5 !== 0){
    console.log("Fizz");
  }
  else if (n % 3 !== 0 && n % 5 == 0) {
    console.log("Buzz");
  }
  else if (n % 3 == 0 && n % 5 == 0) {
    console.log("FizzBuzz");
  }
  else {
    console.log(n);
  }
  n++;
}

Chessboard:

let size = 10;
let line = "# ";
let line_odd = " #";
let i = 0;
for(let i=0; i < size; i++){
  if (i % 2 == 0){
    while(line_odd.length<size){
      line_odd+=" #"
    }
    console.log(line_odd);
  }
  else {
    while(line.length<size){
      line+="# "
    }
    console.log(line);
  }
}

#174

Seems I solved this differently than most. I didn’t use console.log…

I contemplated using continue but decided to reorder the conditionals. You wouldn’t believe what held me up the longest… I had “and” instead of &&. That’s what I get for decades of writing Pascal.


#175

FizzBuzz
for (i=1;i<=100;i++)
{
if ((i % 3)==0)
{
if ((i % 5)==0) {console.log(“fizzbuzz”);}
else {console.log(“fizz”);}
}
else if ((i % 5)==0){console.log(“buzz”);}
else {console.log(i);}
}
//I can’t believe this one really weeds out a significant number of programmers.

Chess Board

let gridSize = 8;
let myline = “”;
for (i = 1; i <= gridSize; i++)
{
if ((i % 2)==1){
for (j=1; j<= gridSize; j++){
if ((j % 2) == 1) {
myline += " "}
else {myline += “#”}}
console.log(myline);}
else{
for (j=1; j<= gridSize; j++){
if ((j % 2) == 1) {
myline += “#”}
else {myline += " "}}
console.log(myline);}
myline = “”;
}
// you can set gridSize to any number you want. I don’t want to figure out how to tie a prompt in there right now.


#176

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

  var toPrint = "#";

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

console.log(toPrint);

  }

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

  if (i%3==0){

    if(i%5==0){
      //If it is both print FizzBuzz
      console.log ("FizzBuzz");
    }

    else {
    //If it is divisible by 3 than do this code
    console.log ("Fizz");
    }

  }
  else if (i%5==0){
    //If it is divisible by 5 and not 3 do this code
    console.log ("Buzz");
  }
  else (console.log (i));

}

The above code displayed the following:
1 2 Fizz 4 Buzz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz etc……

substituting “Fizz” if number divisible by 3
substituting “Buzz” if number divisible by 5
substituting “FizzBuzz” if number divisible by both 3 and 5

ChessBoard


#177

ChessBoard


#178

I know these aren’t the correct solutions, but they are what I got my first time around solving them. For instance, I am aware that I didn’t even use a /n on the chess board exercise and it’s not scalable yet. I’m excited I got my results to look how they should, nonetheless.

  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

     var printFizz = "Fizz";
     var printBuzz = "Buzz";
     var printFizzBuzz = "Fizz Buzz";
    
     for(n = 0; n <= 100; n++){
       if(n % 3 == 0)
         console.log(printFizz);
       if(n % 5 == 0)
         console.log(printBuzz);
       else {
         console.log(n)
       if(printFizz && printBuzz)
         console.log(printFizzBuzz);
       }
    
  3. Chessboard

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

#179

FIZZBUZZ

document.write("<br><br>"+ "TEST 2:  fizzbuzz" + "<br><br>")
    for (i=1; i <= 100; i++)
    {
      if ((i%3) == 0 &&  (i%5) == 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);
          }
        }
      }
    }

CHESSBOARD

document.write("<br><br>"+ "TEST 3:  chessboard v2" + "<br><br>")
var height = 8;
var width = 16;

//print chessboard
for (i=1; i <= height; i++)
{
    for (j=1; j <= width; j++)
    {
      if ( (i%2) == 0)
      {
        if ((j%2) == 0)
        {document.write('&nbsp;');}
        else
        {document.write('#');}
      }
      else {
        if ((j%2) == 0)
        {document.write('#');}
        else
        {document.write('&nbsp;');}
      }
    }
    document.write('<br>');
}

#180
  1. Looping and triangle

     let stringToPrint = "#";
     for (let index = 0; index < 7; index++) {
         console.log(stringToPrint);
         stringToPrint += "#";
     }
    
  2. FizzBuzz

     for (let i = 1; i < 101; 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

     let boardSize = 20;
     let board = "";
     for (let i = 0; i < boardSize; i++) {
         for (let j = 0; j < boardSize; j++) {
             if (i % 2 == 1) {
                 if (j % 2 == 0) {
                     board += "#";
                 } else {
                     board += " ";
                 }
             } else {
                 if (j % 2 == 1) {
                     board += "#";
                 } else {
                     board += " ";
                 }
             }
         }
         board += "\n";
     }
     console.log(board);
    

#181

Looping a triangle

for(var row = 0; row < 7; row++){
var toPrint = “#”;
for(var column =0; column<row; column++){
toPrint += “#”;
}
console.log(toPrint);
}

FizzBuzz

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

Chessboard

  var size = 8;

  var board = "";

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

#182

here’s what I have so far. The browser keeps crapping out on me, so here I go with it

Triangle

Second Project

var num_row = 8;

for(var row = 0; row < num_row; row++){
var print_to = “#”;
for(var column = 0; column<row; column++){
print_to += “#”
}
console.log(print_to)
}

Chessboard (this one was a bitch)

Second Project

var num_row = 8;

for(var row = 0; row < num_row; row++){
var print_to = “#”;
for(var column = 0; column<row; column++){
print_to += “#”
}
console.log(print_to)
}

Fizzbuzz

for(var num = 0; num < 100; num++){
  if(num % 3 == 0)
  console.log="fizz";
  else if(num % 5 ==0)
       console.log="buzz";
    else if(num % 15 ==0)
          console.log="fizzbuzz";

else     console.log="num";
}

Any constructive criticism is welcomed. I can use the feedback/help


#183

FizzBuzz
for (let 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);
}
}

Chessboard
let pair = " # # # #";
let impair = "# # # # ";
for(let i = 0; i < 8; i++) {
if(i % 2 == 0) {
console.log(pair);
} else {
console.log(impair);
}
}


#184

EXERCISES
1.For Loop
for ( triangle = “#”; triangle.length < 8; triangle+="#") {
console.log(triangle);
}

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

if (i % 3 === 0 && i % 5 === 0) {console.log(“FizzBuzz”);}
else if (i % 3 === 0 && i % 5 != 0) { console.log (“Fizz”);}
else if (i % 5 === 0 ) { console.log(“buzz”);}

else {console.log(i);}
}
3.
Need Help!


#185

I admit for the first exercise I was able to find the solution on my own.

The second and third exercise I had to look at the solutions.

I only made to 1, 2, Fizz and my program stopped because I didn’t know how to display “Fizz”.

And I made a grid containing # but it didn’t correctly for an 8 x 8 grid.