Chapter 2 Exercises

Hello @Malik,
Thank you for those words! I’d give you multiple hearts for this reply as well! :smile:

I am very honored by this. Thank you again sir! :pray:

1 Like

Triangle Loop Exercise

for (var line = "#"; line.length < 8; line += "#")
        console.log(line);

Chessboard Exercise


let chessBr = " # # # #"
let chessBl = "# # # #"
for (let i = 0; i < 4; i++) {
  console.log(chessBr)
  console.log(chessBl)
  }

FizzBuzz Exercise

for (let number = 1; number <= 100; 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)

     }

}
1 Like

Headache!! Managed to do FizzBuzz quickly, for some reason, I just knew what I needed to do on that one… Chessboard was a different matter. After a couple of days, I gave in and looked at Google and the Forum… finally after starting again with a clear head, the penny dropped on the logic and I flushed out the use of a comma not a semi colon!

Here’s the code:

// LOOPING A TRIANGLE TASK

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

// FIZZBUZZ TASK

      var number = 0;
      while (number <=100) {
        // if the number is divisible by 3 and 5 show BuzzFizz
        if ((number % 3==0) && (number % 5==0)) {
          console.log("FizzBuzz")}
        // if the number is divisble by 3 show Fizz
        else if ((number % 3==0)) {
          console.log("Fizz")}
        // if the number is divisible by 5 show Buzz
        else if ((number % 5==0)) {
          console.log("Buzz")}
        else {
          console.log(number)}
        number++;
      }

// CHESSBOARD TASK

var size = 8; //this is the value that can change to make the chessboard a different size
var string = ""; //string we are building, will add " ",# or new line to it

for (var row = 0; row < size; row++) {   /*outer loop - we add new line to seperate the rows*/
  for (var col = 0; col < size; col++) { /*inner loop - it adds a " " or a # to the line*/
    if ((col + row) % 2 == 0)  /*col from inner loop, row from outer loop. col will = size before row increases*/
      string += " ";  // even so " " is added
    else
      string += "#";  // odd so # is added
  }
  string += "\n"; //outer loop added the new line
}
console.log(string); //last action, shows the completed string
1 Like

ERXERCIES 2 CHESSBOARD

Hi together, I posted this code a few days ago on the
" I am stuck " section in the forum and thecil advised
me to post a replay here.Capture d’écran 2020-08-16 à 16.42.17
‹4

I made some changes on my code , but I didn’t have the expected result.

Instead it gave this
Capture d’écran 2020-08-16 à 16.34.33

So well… I am stuck and I need some helpful advice.

Thanks in advance for your help and advices.

    https://forum.ivanontech.com/t/chapter-2-exercises/3078/1074?u=max_wolflife

      let f="FIZZ";
      let b="BUZZ";
      for (x=0; x<=100; x++){
      if (x % 3 == 0 && x % 5 == 0) console.log (f+b);
      else if(x % 3 == 0) console.log(f);
      else if(x % 5 == 0) console.log(b);
      else console.log(x)
      x++
      }
1 Like

… I cant do it. Ivans example doesn’t even do anything on my Website for some reason, Clearly ive done something wrong…

Absolutely stuck and ready for throwing the laptop off the wall. :rage:

I forgot a semi-colon to stop the loop and my computer crashed LOL. Lesson learned - details are vital!!

1 Like

Hi @Amri2020, Hope you’re having a wonderful day.
Well, while going through your code, I could find out some issues in the syntax.

First, the if statement brackets are wrongly placed. The console.log("#") should come after the bracket starts.

Second, I see the line
for(let squareLine=0; squareLine<=8; "\n");
Here the third argument in the for statement is a "\n" which is incorrect. If you do not manipulate the “squareLine” to the next number, it will never reach the condition of squareLine<=8, this will result in an infinite loop and thereby crashing your compiler.

Third the syntax let= "\n" is also incorrect, you need to give the variable name first for a let statement. For example - let line = "\n";

Lastly, keeping aside the syntax, the logic of flow is incorrect as well. I suggest you go through this post for clarity. It is well documented.


Or you could check out the animation that I provided for better understanding in the below post.

Hope this helps.

Happy Learning! :slight_smile:

1 Like

@Rob_McCourt , If you need help, please paste the code here so that we can help you out sir. :slight_smile:

Hi @max_wolflife, There’s a tiny mistake in your code which completely changes your output answer. Let me know if you find it, brownie points if you do it yourself! :wink: If not, let me know :slight_smile:

1 Like

Will do pal. Just taking a wee day break. I have another big problem i need help with . can i DM You. its regarding a wallet address and retrieving they funds from it.

over 18k lost from someone i know :(.

@Malik

<script>

// WRITE EXERCISE CODE

var num_rows = 7;
for(var row = 0; row < num_rows; row ++){
console.log(“row number” + row);
}

    <script>

    // WRITE EXERCISE CODE

var num_rows = 7;
for(var row = 0; row < num_rows; row ++){
  console.log("row number" + row);
}


</script>

Looping a triangle.

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

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

This is definitely not a beginners’ course. There is no way I could have completed these exercises with what I have learned so far from the book and the modules. I repeatedly had to refer to outside sources, and even now I’m not confident I could complete them again if required. It would be interesting to know how many people quit at this point.

2 Likes

Hi @Brijay, Well these exercises were meant to make you figure out answers on your own by implementing various ways. When it comes to students, most of them struggle a little in the start, but they quickly gain confidence once they achieve the thought process behind such questions. I hope it will be the same for you.

Hi @Rob_McCourt, could you also provide context as to which exercise you were trying to solve. If you’re having hard time even starting, please refer to other posts above. If you have doubts after that as well, please reach out here again.

Happy Learning! :slight_smile:

1 Like

There you go (:

let f=“FIZZ”;
let b=“BUZZ”;
for (x=1; x<=100; x++){
if (x % 3 == 0 && x % 5 == 0) console.log (f+b);
else if(x % 3 == 0) console.log(f);
else if(x % 5 == 0) console.log(b);
else console.log(x);
}

I had to walk away for a couple days. Still stuck on this one

1 Like

Here is the place to discuss the exercises at the end of chapter two.

These exercises are a stretch for me, but the solutions from other students make sense and I was able to follow their logic. It will take some practice to get the solution on my own.

//exercise 'Looping A Triange’

 var rrows = 7;

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

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

//exercise 'FizzBuzz’

for(var n=1; n<=100; n++){

  if(n % 3==0 && n % 5==0)
  console.log("FizzBuzz \n");

  else if(n % 5==0 && n % 3!=0)
  console.log("Buzz \n");

  else if(n %3==0)
  console.log("Fizz \n");

  else
  console.log(n + "\n")
}

<br />

//exercise 'Chessboard’

//My second attempt to do alone - pretty much did.

  var size = 8; hgrid = size; vgrid = size; board = " "
    for(var vert = 1; vert <= hgrid; vert++){
      for(var hort = 1; hort <= vgrid; hort++){
        if((vert + hort) % 2 == 0) board += " ";
        else board += "#"
      }
  board += "\n"
  }
  console.log(board)

1 Like

The struggle is real, but i managed to understand the logic behind the code. Sometimes ; or { can screw you little bit but keep hustling.

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

2.FIZZBUZZ

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

3.TRIANGLE

var numRow=7;
    for(row=0;row<numRow;row++){
      var toPrint="#"
      for(column=0;column<row;column++){
        toPrint+="#"
      }
      console.log(toPrint)
    }
1 Like

Somehow, the console output of Brave browser compacts the lines and didn’t let me see stuff like the triangle or the chessboard, so I wrote the code in order to insert it on a HTML and print the result on your browser. It was great to practice modifying HTML from the js script.

Here’s my solution for the triangle:


let pound = "#";

for (var i = 1; i <= 7; i++)
{
  document.write("<h1>" + pound + "</h1>");
  pound = pound + "#";
  //nope --> pound.length++;
}

And here’s the chessboard. I made space double for visual clarity.

/*
  write a chessboaard in a given num + num grid
*/

let boardLength = 8;
let spc = "&#160;&#160;", pound = "#"
let tilt = true; //Tilts from writing a # to writing a space and viceversa
for (var i = 0; i < boardLength; i++) {
  document.write ("<h1>");
  for (var j = 0; j < (boardLength); j++) {
    if (tilt) {
      document.write(spc);
    }
    else {
      document.write(pound);
    }
    tilt = !tilt;
  }
  document.write ("</h1>");
  tilt = !tilt;
}

FizzBuzz didn’t need to write HTML since it was shown properly in the console:

let num = 1

while (num<=100) {
  if (num % 3 == 0 && num % 5 == 0) { //2nd version add
    console.log("FizzBuzz");
  }
  else if (num % 3 == 0) {
    console.log("Fizz");
  }
  else if (num % 5 == 0) { //Instead of (num % 5 == 0 && num % 3 != 0), which does nothing extra.
    console.log("Buzz");
  }
  else {
    console.log(num);
  }

  num++;
}

That’s it. It was fun, and the solutions given by the book author were very interesting.

1 Like

Really helpful seeing other people’s answers. I couldn’t solve it myself but have been playing with the code of those whos work and trying to understand the moving parts.

1 Like