Chapter 2 Exercises

//Triangle
let star = ‘’;
let counter = 0;
while (counter <7) {
star += ‘*’;
console.log(star);
counter++;
}

//fizzbuzz
for (input = 1; input <=100; input++) {
if (input % 3 == 0 && input % 5 == 0){
console.log(‘fizzbuzz’);
} else if (input % 3 == 0) {
console.log(‘fizz’);
} else if (input % 5 == 0) {
console.log(‘buzz’);
} else {
console.log(input);
}
}

//Chessboard(1st attempt, does not follow grid rules)
let width = prompt(“Please insert width of chessboard”);
let height = prompt(“Please insert height of chessboard”);
for (x = 1 ; x <= height; x++) {
if (x%2 == 0) {
let empty ="";
for (y = 1; y <=width; y++){
empty+= " #";
}
console.log(empty);
} else {
let empty ="";
for (y = 1; y <=width; y++){
empty+= "# ";
}
console.log(empty);
}

//ChessboardV2(with some help from stack overflow)
let widthV2 = prompt(“Please insert width of chessboard”);
let heightV2 = prompt(“Please insert height of chessboard”);
for (h = 1; h <= heightV2; h++) {
let emptyV2 = “”;
for (w = 1; w <= widthV2; w++)
if ((w + h) % 2 == 0) {
emptyV2 +=" “;
} else {
emptyV2 +=”#";
}
emptyV2 += “\n”;
console.log(emptyV2);
}
}

1 Like

Hi Ben,
I came to the same solution than you did. I just skipped your last if-statement and put the console.log(i) outside the loop.
Also can’t figure out the code from the book but, hey, what’s important is the result, right? I’m sure we’ll learn how to simplify things later on.
Cheers,
Robert

1 Like

FIZZBUZZ
fizzbuzz

1 Like

Alright those were tough on me!

Triangle Loop : Well, it was in the video.

FizzBuzz : Ended up figuring it out on my own but I can see different ways of coding it that are more compact than mine.

Chessboard : I had to look up the solution.

Lots to learn still… lots to get used to.

Very happy with learning so far. :smiley_cat:

1 Like

This was my solution for the triangle:

var stars = “”
var count = Number(0)
while (count<7){
count += 1;
stars += “*”;
console.log(stars)}

This was my FizzBuzz solution:

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

For the checkerboard:

var size = Number(window.prompt(“Please input board size”));
var board = “”;
for (var row = 0; row < size; row += 1)
{
for (var column = 0; column < size; column += 1)
{
if ((row + column) % 2 == 0)
board += " ";
else
board += “#”;
}
board += “\n”;
}
console.log(board);

1 Like

Looping Triangle

var pound = "";
for (let counter = 0; counter < 7; counter++){
     pound += "#";
     console.log(pound);
}

FizzBuzz

for (let 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");
}
   console.log(counter);
}

Chessboard

var size = 8;
var string = "";
for (let rows = 0; rows < size; rows++){
     for (let columns = 0; columns < size; columns++){
          if ((rows + columns) % 2 == 0){
              string += "";
       } 
         else {
              string += "#";
}
}
string += "\n";
}
console.log(string);
1 Like

Is there a way to give someone a referral code?

Is there a referral code for signing someone up to the Academy?

  1. Triangle Loop

let hashtag = “”;
let counter = 0;

while(counter < 7)
{counter ++;
hashtag += “#”;

console.log(hashtag)}

  1. FizzBuzz

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

if (num % 3)
{console.log(num,“Fizz”)}

else if (num % 5)
{console.log(num,“Buzz”)}

else if (num % 3 == num % 5)
{console.log(num,“FizzBuzz”)}

}

  1. Chessboard

let square = 8;
let board = “”;

for(a = 0; a < square; a++) {

for(b = 0; b < square; b++) {

if ((a + b) % 2 == 0)

board += " ";

else board += “#”;
}
board += “\n”;
}

console.log(board);

1 Like
<html>
<head>
<title> Welcome </title>
</head>
<body>
<script>

var numrows = 7;
for(var row = 0; row < numrows; row++){
  var toPrint = "#";

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

</script>
</body>
<body>
<script>

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

</script>
</body>
<body>
<script>

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

</script>
</body>
</html>
1 Like

Looping a traingle

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

FizzBuzz

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

ChessBoard

var size = 8;
for (var i = 0; i < size; i++) {
var str = “”;
if (i % 2 == 0) {
str = " ";
}
for (var j = 0; j < parseInt(size/2); j++) {
str = str + "# ";
}
console.log(str);
}

1 Like

Yikes! The material is getting challenging:)

for (let n = 1; n<=100; n++){
let output = n;

if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;

console.log(output || n);

}

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

CHESSBOARD
different approach without newline character but almost same result (dividing lines in console disturbing a bit)

Xnip2021-05-17_10-47-57

1 Like

Looping a Triangle - Example 2.1:

//declare output as empty string
let output ="";
//start a for loop with a counter that will cause the loop to repeat 8 times.
for(let n = 0; n < 7; n++){
  // add a # with each successive output
  output += "#";
  //write the output to a new line.
  console.log(output);
}

FizzBuzz - Example 2.2:

I can see that my version of this program is not as smart as the answer. I did not think of using the || operator in the way they did, so I didn’t want to take credit considering my alternate way works, though less efficiently.

// create a for loop that will count to 100.
for (let foo = 1; foo <= 100; foo += 1) {
  // condition handling values divisible by 3.
  if (foo % 3 == 0) {
    console.log("fizz");
  // condition handling values divisible by 5.
  } else if (foo % 5 == 0) {
    console.log("buzz");
  // condition handling values not divisible by 3 or 5.
  } else {
  // output the nondivisble value as is.
  console.log(foo);
 }
}

Chessboard - Example 2.3:

// s determines the size of the root of the square
let s = 8;
//out loop that will repeat for each row(r).
for (let r = 0; r < s; r++){
  //wipes the output after each line.
  let out = "";
  //inner loop that builds each rows output.
  for (let c = 0; c < s; c++){
      	//instructions for the condition that the counter is on an even number.
     	if (c % 2 == 0){ 
          out += " ";
        //instructions for the condition that the counter is on an odd number.
        } else {
          out += "#";
        }
    }
  //output the line and continue the loop on the next row, until complete.
  console.log(out);
}

1 Like

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

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

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

I notice a number of variations but I’m happy with what I came up with.

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

I honestly had to cheat this one a bit. Started down the rabbit hole of function defaultGrid and confused the heck out of myself haha.

1 Like

Triangle:

    <script type="text/javascript">
      let toPrint = "#";
      for(let count = 1; count <= 7; count++){
          console.log(toPrint);
          document.write("<h3>"+toPrint+"</h3>");
          toPrint += "#";
      }
    </script>

FizzBuzz:

<script type="text/javascript">

      let fizz = function(num){
        if (num%3==0){
          console.log("Fizz");
          document.write("Fizz");
          return true;
        }
        return false;
      }

      let buzz = function(num){
        if (num%5==0){
          console.log("Buzz");
          document.write("Buzz");
          return true;
        }
        return false;
      }

      for(let a = 1; a <= 100; a++){
        document.write("<p>");
        if(!(fizz(a)|buzz(a))){
          console.log(a);
          document.write(a);
        }
        document.write("</p>");
      }

    </script>

Chessboard:

<script type="text/javascript">
      let boardSize = 9;
      let altSymbol = false;
      let line = "";

      for(let i = 0; i < boardSize; i++ ){
        document.write("<p>");
        for(let j = 0; j < boardSize; j++ ){
          if(altSymbol){
            document.write("#");
            line += "#";
          }else{
            document.write("_");
            line += "_";
          }
          altSymbol = !altSymbol;
        }
        document.write("</p>");
        console.log(line);
        line = "";
        if(boardSize%2==0){
          altSymbol = !altSymbol;
        }
      }
    </script>
1 Like

Triangle:

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

FizzBuzz using while loop:

//Fizz Buzz
  var count = 1
  while(count<101){
    var out = ""
    if((count%3)==0){
      out += "Fizz"
    }
    if((count%5)==0){
      out += "Buzz"
    }
    console.log(out||count)
    count++
  }

Chessboard using While loops (because that’s how I thought of it first (first iteration was complete gibberish and did nothing :sweat_smile:)) and then with For loops:

  //Chessboard
  var space = prompt("First character:", " ")
  var hash = prompt("Second character:", "#")
  var gridh = parseFloat(prompt("Grid height:", 8))
  var gridw = parseFloat(prompt("Grid width:", 8))
  var h = gridh
  var grid = ""
  if ((gridh===Infinity)||(gridw===Infinity)){ //you can make it run 9,007,199,254,740,991 times if you want though
    gridh = 0
    gridw = 0
  }
  while((h>0)&&(gridw>0)){
    var w = gridw;
    while(w>0){
      if((h+w)%2===1){
        grid += hash;
      }
      else {
        grid += space;
      }
      w--;
    }
    grid += '\n';
    h--;
  }
  console.log(grid||"Use a number above zero next time! And not Infinity!")

  grid = "" // cleaning up for the For loop version

  for (var h=gridh; (h>0)&&(gridw>0); h--){
    for(var w=gridw; w>0; w--){
      if((h+w)%2===1){
        grid += hash;
      }
      else {
        grid += space;
      }
    }
    grid += '\n';
  }
  console.log(grid||"Use a number above zero next time! And not Infinity!")

The if ((gridh===Infinity)||(gridw===Infinity)) conditional protects against setting the rows or columns to infinity. By checking for ( (h>0) && (gridw>0) ) the loops are protected against 0x0 grids and grids with negative dimensions that would make the loops run until the variables w and h count down to -9007199254740992. Putting grid||"Use a number above zero next time! And not Infinity!" in the console.log method allows for an ‘error’ message to be displayed when the grid is not a valid size.

For loops are more compact but according to stackoverflow, an old oracle blog and another guy’s blog While loops offer the fastest execution speeds.

1 Like

LOOPING A TRIANGLE

var row, col;
var char = "#";
var print = "#";

for(row = 0; row < 7; row++){
  for(col = 0; col < row; col++){
    print += char;
  }
  console.log(print);
  print = "#";
}

FIZZBUZZ

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

  else if(count % 5 == 0){
    console.log("Buzz");
  }

  else{
    console.log(count);
  }
}

CHESSBOARD

var nRow, nCol;
var size = 8;
var output;

for(nRow = 0; nRow < size; nRow++){
  output = "";
  for(nCol = 0; nCol < size; nCol++){
    if((nRow + nCol) % 2 == 0){
      output += " ";
    }
    else{
      output += "#";
    }
  }
  console.log(output + "\n");
}I 

Does anyone know what the || in console.log(output || n) does? I saw it in the FIZZBUZZ solutions.

1 Like

Looping a triangle
for (line = “#”; line.length <= 7; line += “#”) {
console.log(line);
}

FizzBuzz
for (number = 1; number <= 100; number++) {
output = “”;
if (number % 3 == 0) output += “Fizz”;
if (number % 5 == 0) output += “Buzz”;
console.log(output || number);
}

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

1 Like