Chapter 2 Exercises

Hi @Chillie
I guess you did the exercise number 1 but you forgot to submit your answer

1 Like

I am completely new to programming and have found these exercises challenging but particularly ‘Chessboard’. Thanks for this explanation which has really helped me understand it even if I couldn’t write the code myself!

1 Like

I was struggling a lot with this and after I have found the solution online, I have spent a half a day trying to understand how it worked. I’m glad my explanation helped you. This course is really a struggle for me.

2 Likes

I figured no need to do it twice.

snippet 1 did not work.

You said “do you copy & paste code in your browser” ? I typed my code into the file. When it did not run, I did copy it to the console (for testing) and also I copied my Atom file into the Eloquent JS (for testing). I should not have to type my test from Atom( or any editor) into Eloquent nor console ?

Snippet 2 does not work.

works

Yes. It still works.

1 Like

it also works like this.

1 Like

Yes. That’s correct.

  • Snippet 1 will not work.

  • Snippet 2 works just fine.

I was trying to demonstrate the difference.

1 Like

Ivo…

I got a response that document.write works in html (versus console.log). Worked great. My code was fine, just needed document.write instead of console.log.

Now to explore the difference between the two. Code worked in console. Code worked in the Eloquent test environment. Why not in a browser ?

Funny thing -=== code assignment wasn’t’ the issue !! Environment was.

Will figure it out !

:blush:

~WRD0001.jpg

image001.jpg

var num_rowz = 7;
for (var row =0; row < num_rowz; row++) {
let symbol = “#”
for (var colm = 0; colm<row; colm++) {
symbol += “#”;
}
console.log("row number " + row + symbol);

}

var k = 0;
for (k; k<=100; k++) {
let result = “”;
if (k % 3==0) result += “Fizz”;
if (k % 5==0) result += “Buzz”;
else k;
console.log(result || k);

}

let size = 8, chessb = “”;
for (let l = 0; l < size; l++) {
for (let w = 0; w < size; w++) {
if ((w + l) % 2 == 0) {
chessb += “#”;
} else {
chessb += " ";
}
}
chessb += “\n”;
}
console.log(chessb);

1 Like

Tks gabba!

In fact, I always had problems at school because I lost myself in the middle of long functions. Hahaha, I think it’s an attention problem.

  <script>

   var x = 8

   var board = ""

   var count = 1

   while (count<=8) {

     for (var contagem = 1; contagem <= x ; contagem++) {

      if (( contagem + count ) % 2 == 0)

       board += " "  ;

      else

        board += "#";

      }

   board +="\n"; count++;
 }

console.log(board)

i did the first one a bit different that Ivan explained in the Video. While he did it to output in the console i outputed on the webpage. Here are my “codes” :smiley:

Triangle;

<script>

    let num=(Number(prompt("vnesi velikost trikotnika;")));
    for(let row=0;row<num;row++)
      {
        for(let line=0;line<=row;line++)
          {
            document.write("<b>"+"#"+"<b>");
          }
          document.write("<br>");
      }
    </script>

Fizzbuzz :

    for(let num=1;num<101;num++)
      {
        if(num%3==0&&num%5==0)
          {
            document.write("FIZZ BUZZ" + "<br>");
          }
      else if(num%3==0||num%5==0)
          {
            if(num%3==0)
              {
                document.write("Fizz"+ "<br>");
              }
            else if (num%5==0)
              {
                document.write("Buzz"+ "<br>");
              }
          }
        else
          document.write(num + "<br>");
      }

Chessboard
Might’ve butchered this one a little bit :smiley: but got it working at the end.

    <script>

    let gridSize=Number(prompt("Prosim vnesite velikost polja; "));
    for(let y=0;y<gridSize;y++)
      {

            if(y%2==0)
              {
                for(let x=0;x<gridSize;x++)
                  {
                    if(x%2==0)
                      {
                        document.write(" &nbsp ");
                      }
                    else
                      {
                        document.write("x ");
                      }
                  }

              document.write("<br>");
              }

          else
              {
                for(let x=0;x<gridSize;x++)
                  {
                    if(x%2==0)
                      {
                        document.write("x ");
                      }
                    else
                      {
                        document.write(" &nbsp  ");
                      }
                }
                document.write("<br>");
              }
    }


    </script>
1 Like

CHECKERBOARD
let boardSize = 8;
let checkSpace = “”;

for (let row = 0; row < boardSize; row++) {
    for (let cursor = 0; cursor < boardSize; cursor++) {
      if ((row + cursor) % 2 == 0) {
        checkSpace += " ";
      }
      else {
      checkSpace += "#";
      }
    }
  checkSpace += "\n";
}

console.log(checkSpace);

FIZZBUZZ
for (let num = 1; num <= 100; num ++) {
outString = “”;
if (num%3 == 0) outString = “Fizz”;
if (num%5 == 0) outString += “Buzz”;
console.log(outString || num);
document.write(outString || num);
document.write("
");

Our first assignment in Eloquent JS was to read first 4 topics. I read first 4 chapters by mistake. And did the exercises. My first solution was less eloquent; I used descriptive variables versus using the looping counters as vars; perhaps from my C experience/ code reviews/ mathematical programming I did (oh so many) years ago. I then redid the exercises like Eloquent. Found them hard to read at first, due to lack of descriptive vars, yet how eloquent. I am understanding why kids today write such short texts “OK” “sure” – how eloquent!

My thanks to @mauro for the console.log versus document.write and to @ivo for his help. I let console.log mess me up when, after earlier lessons, I wrote that console.log is for debugging/ testing. But I learned a lot in my hours of seaching.

2 Likes

Looping Triangle

    <script>
    
        //print additional hash every ittiration
        for(let hash = "#"; hash.length<= 7; hash+="#") {
        
        document.write("<p>" +hash+  "</p>");
        
    }
    
    </script>

FizzBuzz

    <script>           
        
        for(counter = 1; counter <= 100; counter++) {
        if(counter % 3 == 0 && counter % 5 == 0){
        document.write("<pre> FizzBuzz </pre>");
        } else if(counter % 5 == 0 && counter % 3 !== 0) {
            document.write("<pre> Buzz </pre>");
        } else if(counter %3 == 0 && counter % 5 != 0) {
            document.write("<p> Fizz </p>");
        }
            else {
            document.write("<p>" + counter + "</p>");
        }
    }
    
    </script>

Chess Board

    <script>
    
        let boardSize = 8, boardHash = "";
            for (let length = 0; length < boardSize; length++) {
                for (let width = 0; width < boardSize; width++) {
                if ((width + length) % 2 == 0) {
                    boardHash += "#";
                    } else {
                    boardHash += "&nbsp";
                            }
                    }
                    boardHash += "<br>";
                    }
            document.write(boardHash);
    
    </script>
1 Like

No problem. I am glad I could help you out. Keep up the good work. :smiley:

@medo_nz I just tested your exercises. Looks like all of your code works as intended. Great job!

2 Likes

Many thanks Mauro :slight_smile:

1 Like

FizzBuzz

for (n = 0; n<100; n++) { var output = " ";
if ( n % 3 == 0) output += “fizz”;
if(n%5 == 0) output += “Buzz”

        console.log(output || n);

}

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