Chapter 2 Exercises

Are you saying that I have to use all three conditions (if, else, and if-else) in the braces?

yes, you have to use them in there https://www.w3schools.com/js/js_if_else.asp take a look at this one as well to have more info about condition and how they work

1 Like

Nothing’s working. I’m still having no success. I keep getting syntax errors in my console. What should I do?

could you post your code here ?

1 Like

What code are you talking about?

I meant, the one that you are trying on console or Atom

1 Like

This is what I have so far:

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

I still cannot figure out what to do next. Please help.

Thank you

@jgentry this is not complete answer, could you implement the rest yourself?

for(var i = 1; i <= 100; i++){
              if(i % 3 == 0 && i % 5 == 0){

                console.log("fizzbuzz");
              }
            }
1 Like

I will do my best. Thank you. If my final answer is definitely not right, at least I did my best.

1 Like

@jgentry no problem even if it is wrong, just try it. And post your code here so that you get a feedback.

1 Like

Here’s the chessboard solution I went with. Again, I have been studying other examples, and am in fact now taking a variety of of other JS courses. I am happy that I understand this now, and am going to move on.


let mySize = 8; let result = '' ;

	let row = 1 ;
	while (row <= mySize ) {
		let column = 1 ;
	while (column <= mySize ) {

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

	}
		column += 1;

}

	result += '\n';
	row += 1;

		}
	console.log(result);

2 Likes

Wow. That was a brain tease. Need some help with the Chessboard, I believe I have figured out how to get the pattern going, but cant figure out how to go to the next line every 8th cell. Any pointers will be helpful. Will go through the forum for further help on that one

LOOPING A TRIANGLE
for (hash = “#”; hash.length <= 7; hash += “#”){
console.log(hash);
}

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

CHESSBOARD
var size = 8;
var hash = “#”
var clear = " "

  for (let x = 0; x < size; x++){
    for (let y = 0; y < size; y++){
      if((x+y)%2==0){
        console.log(clear);
      } else {
          console.log(hash)
        }
      }
    }

Triangle

var str = "";
while (str.length < 7) {
    str += "#";
    console.log(str);
}

FizzBuzz

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.toString());
    }
}

Chessboard

var size = 8;
var flip = false;
for (var i = 0; i < size; i++) {
    var str = "";
    for (var j = 0; j < size; j++) {
        if (!flip) {
            str += " ";
        } else {
            str += "#";
        }
        flip = !flip;
    }
    console.log(str);
    flip = !flip;
}
1 Like

These exercises were challenging didn’t know where to start. Had to lean on some prior responses just to get a hint. Didn’t realize there were so many ways to write one thing. And there were a lot more functions for the same thing than I was expecting. A lot like all the words we have for the same thing in English haha.

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

I’m confused by the row < num_rows syntax. Does the it mean its proceeding in a orderly fashion? And one of the ‘toPrint’ has a var in front of it and the other ‘toPrint’ does not. When I added a var next to the other ‘toPrint’ I got an error. Can someone explain why?

FIZZBIZZ

for(var b=1; b<100; b++){
  if (b % 5 == 0 && b % 3 == 0) document.write("FizzBizz <br>");
    else if(b % 5 == 0) document.write("Buzz<br>");
    else if(b % 3 == 0) document.write("Fizz<br>");
    else document.write (b +"<br>")

}

Had trouble determining the different else and if statements but eventually got it to work. I was confused by some using output and some using document.write. I looked up and saw document.write was for html. Is there any real difference or advantage between the two?

CHESSBOARD
var b=8
var a1="# # # #";
var a2=" # # # #";
for (i=1; i<=b; i+=2){
console.log(a2+"\n");
console.log(a1+"\n");
}

1 Like

This is the solution to the Fizz Buzz (the first task is without the third “if” condition
for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
if (n % 3 == 0 & n % 5 == 0) output += “FizzBuzz”;
console.log(output || n);
}

I was not able to solve the chess board. Trying to sort out the solution if finally got it.

1 Like

I just started learning this, unfortunately because of it, I don’t have a lot of knowledge based on this and if there are other ways to put those codes inside Atom. In other words, I just got guided from Eloquent and the way the codes should be. So…

  1. Looping a Triangle
    for (let line="#";line.length<8;line+="#")
    console.log(line);//Loop Triangle

  2. Fizzybuzz
    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);}//Fizzbuzz

  3. 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);//Chessboard

@minman In forum communication, it helps a lot if you use code blocks like this. take a look at picture below.

1 Like

@pepemadrid n forum communication, it helps a lot if you use code blocks like this. take a look at picture below.

1 Like

@Kristhofer_Drost1 In forum communication, it helps a lot if you use code blocks like this. take a look at picture below.

1 Like