Chapter 2 Exercises

Am I alone in feeling the exercises don’t correspond appropriately to the material covered? Have no problem understanding the videos and readings but totally lost on the exercises, cannot answer any of them correctly without looking at the solutions and the solutions generally contain something I’m completely unfamiliar with or wouldn’t have expected to be included. Very discouraged at the moment.

1 Like

Hi @Marklar, hope you are great.

Sad to hear this, If you are not familiar with programming, I could suggest to start from the begining, the JavaScript course teach the entire basics for HTML and JS, also we provide simple exercises that can be used to practice, what you need is to practice enough until you start get familiar with the syntax and methods.

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

Carlos Z.

1 Like

Thanks for the reply. I’ll keep plugging away, just blew a bit of a fuse today :sweat_smile:

1 Like

Triangles:
var hash = " ";

var counter = 0;
while (counter <8) {
hash = hash + “#”;

console.log(hash);
counter++;

FizzBuzz
var counter = 1;
while (counter < 101) {

if (counter %15 == 0)
console.log(“FizzBuzz”);

else if (counter %5 == 0)
console.log(“Buzz”);

else if (counter %3 == 0)
console.log(“Fizz”);

else
console.log(counter);
counter++;
}

ChessBoard
I couldn’t work out how to do a “scalable version”. My 8 x 8 worked:

for (let n = 1; n <= 8; n++) {
let output = " ";
if (n % 2 == 0) output += "# # # # “;
if (n % 2 != 0) output += " # # # #”;
console.log(output || n);
}

I read the " model" answer and understood it, except I couldn’t work out the meaning of the expression “board += “\n”;” Help please !

Looping a triangle:

        let hashtag = "#";
        while (hashtag.length < 8) {
          console.log(hashtag);
          hashtag += "#";
        }

FizzBuzz:

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

Chessboard:

        let grid = "";
        let size = 8;
        let height = 0;
        let width = 0;

        for (let counter = 0; counter < size; counter++) {
          while (height < size) {
            while (width < size) {
              if((height+width) % 2 === 0) {
                grid += " ";
              } else {
                grid += "#";
              }
              width++;
            }
            grid += "\n";
            height++;
            width = 0;
          }
        }
        console.log(grid);
1 Like

This expression basically translates into
board = board + "\n"

Hope this helps. :slight_smile:

1 Like

well ahhhhhh - as a complete beginner i found this mega hard.

I was at this point last week and had a flip out & decided this wasn’t for me. I woke up the next day and went away and did lots of other javascript for beginners video tutorials.

i’ve just come back to this now and don’t get me wrong its still hard and i’ve had to look at other peoples script to fix my own but at least its all starting to make sense now !

LOOPING A TRIANGLE:

var txt = “”;
var count = 0;
while(count < 9){
console.log(txt+="#");
count++;
}

FIZZBUZZ

(not sure if this the best way to do it but it works and makes sense in my head!)

var count = 1;
var f = “FIZZ”;
var b = “BUZZ”;

for(list = 1; count < 100; list++)
{
if(list % 5 == 0 && list % 3 == 0) console.log(f+b);
else if(list % 3 == 0) console.log(f);
else if(list % 5 == 0) console.log(b);
else console.log(count);
count++
}

CHESSBOARD:

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

If anyone has any recommendations of tutorials they’ve seen which has helped please let me know… thanks :v:

2 Likes
1. Triangle:
    for (i=0; i<=7 ; i++){
       text="#"; 
       for (j=0; j<i ; j++){
          text += "#";
       }
       console.log(text);
    }

2. FizzBuzz
   for (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);
       }
   }

3. checkerboard:
    size = 8;
    for (i = 0 ; i < size; i++) {
       text = " ";
       for (j=0 ; j < size; j++) {
          if ((i + j) % 2 == 0) {
              text += "#";
          } else { 
              text += " ";
          }
       }
       console.log(text);
    }
1 Like
Triangle
for(row="#"; row.length<8;row+="#")
console.log(row);

FizzBuzz
for(let i = 1; i <= 100; i++){
let toPrint ="";
if(i % 3 == 0) toPrint += "Fizz";
if(i % 5 == 0) toPrint += "buzz";
console.log(toPrint || i);
}

Chess
var size = 8;
let table = "";
for(let row = 0; row < size; row++) {
for(let col = 0; col < size; col++) {
if((row+col) % 2 === 0){
 table +=" ";
 }
else{
 table +="#";
 }
}
    table += "\n";
}
console.log(table);

To be honest for the last one i had to use a bit of forum because i was stuck on the second one for the 3hours and when i got stuck on third one i just found code on forum and typed it by my self and researched little bit for maybe shorter code but thats it

1 Like

Triangle loop

var num_rows = 7
for (var row = 0; row < num_rows; row++) {
var print = “#”;
for (var column = 0; column < row; column++) {
print += “&”;
}
console.log(print);
}

FizzBuzz Loop

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

Chessboard

let size = 8;
let 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);

1 Like

This was not easy, i fell to sleep haha but i tried it again and again.
I read a lot on the internet and the comments here.
I still need to learn more.
i realize that i have trubbel whit () and {}. i always forgot or its hard to know where to put them.
I cheated about the % line, i need it to know.
Namnlös1

1 Like

TRIANGLE

var stars = " ";
var b = “

var counter = 0 ;
while (counter < 9 ) {
stars = stars + “*”;
document.write(stars, b);
console. log(stars);
counter++;
}

FizzBuzz

for (var i=1; i < 101; i++) {
if (i % 15 == 0) {
document.write("FIZZBUZZ
");
}
else if (i % 3 == 0) {
document.write("FIZZ
");
}
else if(i % 5 == 0) {
document.write("BUZZ
");
}
else {
document.write(i + “
”);
}
}

Chessboard

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

1 Like

Looping Triangle

<script>

for (let loopingTriangle = "#"; loopingTriangle.length < 7; loopingTriangle += "#") {
  console.log(loopingTriangle);
}
</script>

Fuzzbuzz

<script>

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

Chessboard

<script>

var a = " #";
var b = "# ";

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

  if (i % 2 ==0) {
    console.log(a.repeat(4));
  }
  else {
    console.log(b.repeat(4));

  }
}


</script>

2 Likes

I cannot solve any of these problems without looking up the answers

2 Likes

Does this count as a solution to Looping A Triangle ?

let hash = "#";
let count = 0;
      
while(count<=6){ 
console.log(hash),count++,hash+="#"
};

First Part of FizzBuzz

let divisable3 = "FIZZ";
let divisable5 = "BUZZ";
let counter = 1;

while (counter<=100){

    if (counter%3 === 0){    
    console.log(divisable3),counter++
    }
    else if (counter%5 === 0){    
    console.log(divisable5),counter++
    }
    else {    
    console.log(counter),counter++
    };
    };

He hey second part

let divisable3 = "FIZZ";
let divisable5 = "BUZZ";
let divisable35 ="FIZZBUZZ";
let counter = 1;

while (counter<=100){

    if((counter%3 === 0) && (counter%5 === 0)){    
    console.log(divisable35),counter++
    } 
    else if (counter%3 === 0){    
    console.log(divisable3),counter++
    }
    else if (counter%5 === 0){    
    console.log(divisable5),counter++
    }
    else {    
    console.log(counter),counter++
    };
    };
1 Like
  1. 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 + “
    ”);
    }

  2. for (var i=1; i < 10; i++){
    if (i % 15 == 0) document.write("FizzBuzz
    ");
    else if (i % 3 == 0) document.write("Fizz
    ");
    else if (i % 5 == 0) document.write("Buzz
    ");
    else document.write(i + “
    ”);
    }

  3. 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

My solutions, I’m not sure how to add spaces in HTML for indentation, no time to look it up, so here we go.

Triangle

var length = prompt("How many rows?");
var l = parseInt(length);
var hash = "#"
console.log(hash);
while (hash.length <= l){
hash += "#"
console.log(hash);
}

Fizzbuzz

var input = prompt("Enter a number!");
var n = parseInt(input);
for (i = 0; i <= n; 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);
}
}

Chessboard

var size = prompt("What is the size?");
var s = parseInt(size);
var board = "";
var boardrow = "";

for (r = 1; r <= s; r++){
if (r%2==0){
for (c = 1; c <= s; c++){
board += " #";
if (c == s){
board += \n
}
else {continue}
}
}
else {
for (c = 1; c <= s; c++){
board += "# ";
if (c == s){
board += \n
}
else {continue}
}
}
}
console.log(board)

1 Like

Hi,
I need some help with ChessBoard.
ChessboardCode3
In image you can see that code is executing, but not printing in next line new characters.
To fix this I add ,count-- in indicated place so it would update my count.
But when I do this code just crashes, console stops working and there are no error messages!
Can you help me with this please? What I am missing?
Thank you!

var chessboard =""; 
var space ="";
var hash ="#";
var newrow ="\n";
var x = 8;
var y = 8;
var count = x*y+y-1;


while (y>1){    
    if (count%2===0){    
    chessboard+=hash,count--}
    else if (count%line===0){chessboard+=newrow,y--,count--}
    else if (count>1){chessboard+=space,count--}
    };
console.log(chessboard);

Practice exercise - Boolean’s & If,Else statements

  1. Quiz

  2. Write a script asking for user input on one or more trivia questions. If you do not know a question, you get a free one here: “What is the name of the capital of Morocco?” (The answer is “Rabat”).

  3. Save the correct answer in a text string.

  4. Write an if-test to check if the user has answered the question correctly. If they have answered correctly, the program should print “ Exactly! ” to the console. If not, the script should print “ Sorry, the answer was ” and then the correct answer you have saved to the console.

What am I doing wrong, It looks identical to me yet there is an error?

Hi, Kamil
I marked with highlighter quotes are different type and this causes error.

2 Likes