Chapter 2 Exercises


#229

Try to refresh the page, it should work.
If it doesn’t work, could you take screenshot and post it?


#230


#231

hmmm did you save the file as html?


#232

i saved it as a textedit file


#233

I think it’s in the settings of Text Edit. This program seems to be able to render HTML. Turn that off:
Open the preferences: TextEdit > Preferences
In the tab “New Document” select format > plain text
Go to the tab “Open and Save”
Check this box: “Display HTML files as HTML code instead of formatted text”
I think that should work for you.


#234

Here is the link for help:


#235

Thank’s a lot, it is working also in chrome now :slight_smile:


#236

Hi,
The code does not execute in google chrome console. Can anyone help me?

Kind regards


#237

Looking at your title, which starts with a capital ‘T’ for “This” but the page you are viewing has a lower case ‘t’ for the same word - based on that i’m assuming it’s because you’re viewing a different file.


#238

Looping a triangle

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

Fizzbuzz

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

Chessboard (8x8 grid)

// Bindings
var size = 8; colPairs = rowPairs = size/2; oddRow = ""; evenRow = ""; chess = "";

// Build Odd and Even Rows
for(i=0; i<colPairs; i++) { oddRow+=" #"; evenRow+="# "; }

// Build and Print Chessboard
for(i=0; i<rowPairs; i++) { chess+=oddRow+"\n"+evenRow+"\n"; }
console.log(chess)

#239

1 Triangle Figure

      var i , j;
      for(i=1;i<=5;i++)
      {
       for(j=1;j<=5;j++)
       {
         if(j<=i)
            {
             var toprint = "#";
             document.write(toprint);
            }
         else
            {
             document.write( );
            }
       }
       document.write("<br>");
      }

2. Fizzbuzz Problem

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

3. ChessBoard

       var i , j;
        for(i=0;i<8;i++)
        {
         for(j=0;j<8;j++)
         {
           if((i + j) % 2 ==0)
              {
               document.write("&nbsp");
              }
           else
              {
               var toprint = "#";
               document.write(toprint);
              }
         }
         document.write("<br>");
        }

#240

As a complete noob to coding - I don’t think one can solve the check-board if this book is the first material you see/use to learn JS (unless you are very gifted and math savvy; and I lack in both departments)
Last time I wrote a line a code was in 1st grade using Basic

I think my biggest achievement was that I saw some answers here than made me understand how to rewrite a functional code for FizBuzz

for (x=0; x<=100; x++){if (x%3===0 && x%5===0){console.log("FizzBuzz")}
else if(x%5===0){console.log("Buzz")}
else if (x%3===0){console.log("Fizz")}
else (console.log(x))}

Moving to CheckBoard (at least I typed it twice manually line by line to understand what it does- but I could not have come with this logic to save my life)

let size = 8
let board = "";

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

:persevere:


#241

Triangle:
for(let i = 0; i<8; i++) {
console.log(’#’.repeat(i))
}

FizzBuzz:
for(let j = 1; j<=100; j++) {
if(j%3==0 && j%5==0){
console.log(‘FizzBuzz’)
} else if(j%3==0){
console.log(‘Fizz’)
} else if (j%5==0){
console.log(‘Buzz’)
} else {
console.log(j)
}
}

Chessboard:
board=[];
for(let r = 0; r < grid; r++){
for (let c = 0; c <= grid; c++){
if (c === grid){
board.push(’\n’)
} else if ((r % 2 === 0 && c % 2 === 0) || (r % 2 === 1 && c % 2 === 1)){
board.push(’#’)
} else {
board.push(’ ')
}
}
}

console.log(board.join(’’))


#242

it works for me…
BTW if you paste code directly, we can cut and paste and troubleshoot easier. Cheers, Mark.


#243

This was a bit tough and to be honest I had to look at some examples for help, however i think i understand the examples now and i was able to make my own stuff afterwards.

Hash Triangle
for (let myrow = “#”; myrow.length <= 7; myrow += “#”) console.log(myrow);

FIZZBUZZ
for (let myrow = 1; myrow < 101; myrow += 1) {
if (myrow % 3 == 0 && myrow % 5 == 0) {console.log(“FIZZY&BUZZY”)}
else if (myrow % 5 == 0) {console.log(“BUZZY”)}
else if (myrow % 3 == 0) {console.log(“FIZZY”);}
else {console.log(myrow)}
}

Chessboard
let size = 8
let place = “”;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x + y) % 2 == 0) {place += " ";}
else {place += “#”;}
}
place += “\n”;}
console.log(place)


#244

1. Triangle
for (i=1;i<=7;i++){ document.write("#".repeat(i),"
"); }

2. FizzBuzz
for (i=1;i<=40;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. Checker-board
counter=8;
for (i=1;i<=counter;i++){
if (i%2==0) { console.log(" “+”# “.repeat(counter)); }
else { console.log(”# ".repeat(counter)); }
}


#245
  1. 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);
}
2. FizzBuzz:

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

if((a%3== 0)&&(a%5==0)){console.log(“FizzBuzz”)}
else if(a%3==0){
console.log(“Fizz”)
}
else if(a%5==0){
console.log(“Buzz”)
}
else { console.log(a)};
}
3. 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);


#246

Triangle Loop
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 + “
”);
}
FizzBuzz Loop
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 + “
”);
}
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 += " ";
else
board += “#”;
}
board += “
”;
}
document.write(board);


#247

// Triangle Exercise

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

// FizzBuzz

    var row = 1
       while (row<=100){

      console.log(row)
      row ++

      if (row % 15 == 0){
        console.log ("FizzBuzz")
      }

if (row % 3 ==0){
  console.log("Fizz")
  row ++
}

if (row % 5 == 0){
  console.log ("Buzz")
  row ++
  }

    }

// ChessBoard

    var size = 8;

    var board = "";

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

#248

Looping a triangle

Here are some versions where you do not need two sets of for…do loops:

txtOutput = “x”;
counter = 0;
while (counter < 7) {
  console.log(txtOutput);
  counter++;
  txtOutput += “x”;
}

txtOutput = “x”;
for (let counter = 0; counter < 7; counter = counter + 1) {
  console.log(txtOutput);
  txtOutput = txtOutput + “x”;
}

txtOutput = “x”;
do {
  console.log(txtOutput);
  txtOutput += “x”;
} while (txtOutput.length <= 7);

FIZZBUZZ

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

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

CHESSBOARD

let txtOutput = “”;
for (let x = 1; x <= 8; x++) {
    if (x % 2 != 0) txtOutput += " ";
    for (let y = 1; y <= 8; y++) {
      if (y % 2 == 0) txtOutput += “#”;
      else txtOutput += " ";
    }
    txtOutput += “\n”;
}
console.log(txtOutput);

let txtOutput = “”;
let xMax = 20;
let yMax = 20;
for (let x = 1; x <= xMax; x++) {
  if (x % 2 != 0) txtOutput += " ";
    for (let y = 1; y <= yMax; y++) {
      if (y % 2 == 0) txtOutput += “#”;
      else txtOutput += " ";
    }
  txtOutput += “\n”;
}
console.log(txtOutput);