Chapter 2 Exercises

//Triangle loop
let size=8;
for(let line="#"; line.length<=size; line+="#"){
    console.log(line)
    }
//FizzBuzz
let max=100, count=0;
while(count<max){
    count++
    let output="";
    if(count%3==0){output+="Fizz";}
    if(count%5==0){output+="Buzz";}
    if(output==""){output=String(count);}
    console.log(output);
    }
//Chessboard
let size=8, output="";
for(let y=0; y<size; y++){
    for(let x=0; x<size; x++){
        if((x+y)%2!=0){output+="#"}
        else{output+=" "}
        }
    output+="\n";
    }
console.log(output)

It took some time, but I figured it out, not sure this is the most efficient code, but it works! (I think :slight_smile: )

FizzBuzz

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

Chessboard

let size = Number(prompt("What grid size do you want?"));
var rLine = "";
for(var b = 0; b < size; b++){

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

1 Like

Totally new to programming but these seemed like the simplest solutions to me.

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

// FIZZBUZZ

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

// CHESSBOARD

  var size = 13
  var even = " #"
  var odd = "# "
  for (length = 1; length < (size/2); length++){
    even += " #"
    odd += "# "
  }
    for (row = 0; row < size; row++){
      if (row % 2 == 0) console.log(even)
      else {console.log(odd)}
   }
2 Likes

Hi @sterlingseah,

Could you also specify what sort of error you are facing? With that we can understand what might be the issue and can help you out.

Happy Learning! :smile:

1 Like

trianle:
for (row = “#” ; row.length <= 7; row += “#”)
console.log(row);

fizzbuzz:
for (var count = 1; count <= 100; 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;
textToPrint = “”;
for(let i = 0; i < size; i++){
for (let j = 0; j < size; j++){
if (i % 2 === 0){
if (j % 2 === 0){
textToPrint += " “;
}
else{ textToPrint += “#”;}
}
else{
if (j % 2 === 0) {
textToPrint += “#”;}
else{
textToPrint +=” ";}}}
textToPrint += “\n”}
console.log(textToPrint);

1 Like

image
image


As you can not see the " " really good on the webpage - I turned it into “_” :slight_smile:

1 Like

Solution to FizzBuzz Excercise:

for( count = 1 ; count < 101 ; 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);
}

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 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 (this would definitely had me with the spacing element) 

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 += “
”;
}
document.write(board);

1 Like

Triangle

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

FizzBuzz
My first solution was this one:

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

    let result = '';

    if (i%3 == 0) { result += "Fizz"; };
    if (i%5 == 0) { result += "Buzz"; };

    if (result == '') { console.log(i); }
    else { console.log(result); };

  };

Then I found in the thread a prettier solution for the last part:
console.log(result||i);

Chessboard

let size = 8;
for(row = 1; row <= size; row++){

    let line = "";
    for (column = 1; column <= size; column++) {

      if ((row + column)%2 == 0) { line += " "; }
      else { line += "#"; };

    };

    console.log(line);

  };
1 Like

Triangle

Fizzbuzz

Chessboard

Looping a triangle

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

FizzBuzz

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

Chessboard

let u= "# # # # “;
let i= " # # # #”;
for (count=0; count<4; count++){
console.log(i);
console.log(u);
}

1 Like

Solution to Triangle Loop Excercise:

for(x=1; x<=7; x++) {

let output = “#”;

if(x>=2) output +="#";
if(x>=3) output +="#";
if(x>=4) output +="#";
if(x>=5) output +="#";
if(x>=6) output +="#";
if(x>=7) output +="#";

console.log(output);
}

1 Like

Solution to Chessboard Excercise:

let spacehash = " #"; let hashspace = "# "; let grid = 10; let reper = grid/2;
for(x=0; x<grid; x++) {
if(x%2 == 0) {
console.log(spacehash.repeat(reper));
} else {
console.log(hashspace.repeat(reper));
} }

1 Like

PYRAMID

    var count = 0;
    var hash = "#";
    while(count<7) {
      console.log(hash);
      count++
      hash = hash + "#"
    }

FIZZBUZZ

    var count = 0;
    var hash = "#";
    while (count<7) {
      console.log(hash);
      count++
      hash = hash + "#"
    }

CHESSBOARD

  const a = " # # # #";
  const b = "# # # # ";
  for(r=1;r<5;r++) {
    console.log(a);
    console.log(b);
  }

Note: This version of CHESSBOARD does not include the binding size = 8 instruction, but have worked all afternoon & wanted to post these. Will Post the variable version when have completed it!!

1 Like

Chessboard:
image

FizzBuzz:

Chessboard:

3 Likes

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

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

Chessboard
let size = 8;

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

Chessboard was extremely difficult for me. I had to watch/read many different sources just to understand the concept.

2 Likes

Yes, very difficult for me too.

2 Likes

Looping a triangle
for (x= 0; x < 7; x++){
var toShow = “#”;
for (y = 0; y <x; y++)
{
toShow+="#";}
console.log(toShow);
}

FizzBuzz
for(n = 0; n < 100; n++){
if(n % 3 == 0) {console.log(“Fizz”);}
else if(n % 5== 0)
{console.log(“Fuzz”);}
else if((n % 3 == 0) && (n % 5 ==0)){console.log(“FissFuzz”);}
else {console.log(n);};
}
Chessboard
let size = 8;
let 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);

2 Likes

There should be exercises for beginners to build the actual code. Kind of like in first grade when you drill through adding and subtract through a bunch of exercises. Step by step.

Like FizzBuzz for example.

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

It makes perfect sense when I see the code, but writing it on my own my head freezes. Sat for over 5 hours trying. The logic was there, correct code wasn’t, mainly because syntax blur, not seeing what goes where and how it’s supposed to be written.

3 Likes

Hello @GusNotFring, hope you are great.

Your code looks good, programming is about discipline basically, your mind freeze when you stop programming, the only way that i know to avoid “mind freezing” is by coding at least 1 hour per day, more practice you made, more fundamentals you will be used to (syntax rules).

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

2 Likes