Chapter 2 Exercises

I see what was needed in the last example to make sure a space was used before the “#” mark then you must use var show3 = “&nbsp”; How tricky my friend. That was not in the lesson.

1 Like

No I got most of what I Iearned to do the program problems from Ivan and Chapter 2. The Fizz Bizz problem took me a while say hour or two because I had syntax issues. But when I looked bask in the Chapter to see how to write my loop and { } statement it worked no problem. Now the board I got to work immediately cause I did everything in the browser console and not with Atom or another editor. It worked at first try with only 3 lines of code. But when I transfer my code to Atom the first two worked but not the chess board because I set up two strings, string1 = “# # # # # # # #” and the second string zi started with a space, string2 = " # # # # # # # #" The Atom compiler would just ignore the space and so I got straight row and columns of spaces and “#”. I could’nt figure it out until I got on the Post to find out you have to use a third string called “&nbsp” no-break space so the compiler will not break and will add your space. Not that was not in Chapter 2 or from the lecture. That was the tricky part. Hang in there. It will start to click after a while of practice. Sometimes you have to read the chapter two or three times and even listen to the lecture two or three times. This is a great course but it is challenging. Stick with it and the light bulb will go off until the next challenge.

1 Like

Hello all, I am attempting the FizzBuzz exercise, and I have managed to write the code for logging Fizz and buzz, but when I try to add the last condition for FizzBuzz (last line in BOLD), nothing seems to change…This is the code below:

for (let num = 1; num<=100; num++){
if(num % 3 ==0){
console.log(“Fizz”)}

else if (num % 5 != 0 && num % 3 !=0 ){console.log(num)}
else if (num %5 == 0 && num % 3 !=0) {console.log (“Buzz”)}
else if (num %5 == 0 && num % 3 ==0) {console.log (“FizzBuzz”)}
}

1 Like

Looping triangle:

<Script>

      var row_num = 7
      for(var row = 0; row < row_num; row++){
        var Print = "#";

        for(var column = 0; column<row; column++){
          Print += "#";
        }
        document.write("<h2>" + Print + " " + "</h2>");
      }

      </script>

FIzzBuzz:

<Script>

for(let number = 1 ; number <= 100; number++) {
    if (number % 3 == 0 && 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)
    }
  }

  </script>

Chessboard:

<Script>

function chessboard(size) {
  var output = "";
  for (let i=0; i<size; i++) {
    for (let j=0; j<size; j++) {
      output += i % 2 == j % 2 ? " " : "#" ;
    }
    output += "\n";
  }
  return output
}

  </script>

Not gonna lie, I really struggled with the chessboard. Also, was absolutely amazed when I saw the answers to them on the eloquent website as they were 1 or 2 lines. I’ve got a loooooooong way to go XD :stuck_out_tongue:

1 Like

Chessboard

<script>
  let size = 8;
  let result = "";

  let row = 1;
  while(row <= size){
    let column = 1;
    while(column <= size){
      if((column + row) % 2 == 0){
        result += " ";
      } else {
        result += "#";
      }
      column += 1;
    }
    result += '\n';
    row += 1;
  }

  console.log(result);
</script>
2 Likes

Greetings @Sedali, The thing about the condition is, it is written after the divisibility of 3 check. So ideally you would want 15 to return as FizzBuzz, but since 15 is already divisible by 3, the first “if” condition gets triggered.
To fix this issue, you could write the FizzBuzz condition as the first condition as shown below –

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

}

Hope this helps.

Happy Learning! :slight_smile:

2 Likes

Yes that makes sense, thank you so much! :slight_smile:

1 Like

Thanks so much for the encouragement, but I feel like I’m being expected to write a story in a foreign language when no one will tell me what the words mean. For me, the lessons seem to be rushed through, so not only can I not work out what the jargon means (call, pass, argument etc.) I cannot get the concept straight in my head before the lesson has moved on to the next topic. I admit I am a total beginner but English is my first language so my difficulties aren’t a translation issue.

I have started watching some beginners’ lessons on YouTube and, as you say, the light-bulb, if not lighting-up, is starting to flicker. Then when I have understood the basics, I will come back to these lessons.

Thanks again for your message.

1 Like

Hello, I am really struggling to understand the logic behind the chess board exercise, in the below code what is the purpose of “\n” and how does the code produce the pattern in both horizontal and vertical directions ?

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 +=”\n";

}

console.log(board);

Thanks in advance.

1 Like

Greetings @Sedali,
Hope you’re having a wonderful evening.

To understand this problem exercise, could you go through the below linked posts. These are in-depth explanations of how they work. Also, the “\n” creates a new line in the string for separating each line.


Please go through them and let me know if you have any doubts.

Happy Learning. :slight_smile:

1 Like

Triangle tree:

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

Boots n’ cats (fizz buzz):

                  for (let n=1; n <= 100; n++) {
                  let output =  “”;
                  if (n % 3 == 0) output += “boots”;
                  if (n % 5 == 0) output += “cats”;
                  console.log(output || n); }

Chessboard (This one is a little off, but I can’t tell why. The top row doesn’t line up in my console.)

                       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

Looks like this…

I could only come up with the Fizz Buzz so far but I am still having some problems with it as some numbers are doubled and I do not know why.

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

Greetings @Lyndon,

Could you also add the “bootscats” condition when the number is a multiple of 15.

I do not see any issues in this implementation. It’s probably because your Dev tools tab was cramped up. Just try to increase the width of your dev tools console, and it should be fine :slight_smile:

Hope this helps.

Happy Learning! :smiley:

1 Like

Greetings @Sebastian_Sporea,

Looks like you are console logging twice buddy. Thats why each iteration is outputting twice in the console.

Hope this helps :slight_smile:

1 Like

Way to hard for me, it took days. Im not used to think so deep and connect all the learned stuff together. Had to use help and research a lot.
FizzBuzz
for (var i = 1; i <= 100; i++){
var output = “”;
if (i % 3 == 0) { output += “Fizz”;}
if (i % 5 == 0) { output += “Buzz”;}
if ((i % 3 == 0) && (i % 5 == 0)) { output += “FizzBuzz”;}
console.log((output) || (i));
}
Chessboard
sizex = 8;
sizey = 4;
var toPrint = “”;
for (var x = 1; x <= sizex; x++){
for (var y = 1; y <= sizey; y++)
if (x % 2 === 0) {
toPrint += "# “}
else {toPrint += " #”
}
toPrint += “\n”
}
console.log(toPrint)

1 Like

Hey Brian

You are welcome. Getting started is always the toughest time but when you get the first program running man it is a great feeling. I also go to utube reinforce and learn new things.

Have you tried “lean html in 12 min.” https://www.youtube.com/watch?v=bWPMSSsVdPkand and learn more html in 12 more min” https://www.youtube.com/watch?v=KJ13lX20FqU,
They we great and also I watch the U tube video on JS for 3.5 hours this guys is really good. https://www.youtube.com/watch?v=Bv_5Zv5c-Ts

Regards

Imhotep

Sent from Mail

Of course. I initially misunderstood what you meant.

for (let n=1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “boots”;
if (n % 5 ==0) output += “cats”;
if (n % 3 == 0 && n % 5 == 0) {output +=“bootsncatsn”}
console.log(output || n);

1 Like
  1. FIZBZZ

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

  1. ChessBoard

`let spaceHashtah = " #"
let hashtagSpace = "# "

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

			if (i % 2 == 0){
				console.log (spaceHashtag.repeat(4))
				else {
					consol.log (hashtagSpace.prepeat (4))
				}
			}`

Triangle Loop

let hashh = "#";
for (let number = 0; number <= 7; number += 1) {
        document.write(hashh + "<br/>");
        hashh += "#";
      }

FizzBuzz Loop

      let fizz = "Fizz";
      let buzz = "Buzz";

      for (let counter = 1; counter <= 100; counter += 1) {

        if ((counter%3) == 0 && (counter%5) == 0) {
          document.write(fizz + buzz + "<hr/>");
          continue;
        }

        else if ((counter%3) == 0) {
          document.write(fizz + "<hr/>");
          continue;
        }

        else if ((counter%5) == 0) {
          document.write(buzz + "<hr/>");
          continue;
        }

        else {
          document.write(counter + "<hr/>");
        }

      }

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

2 Likes