Chapter 2 Exercises


#330

Hi.

So I also managed to successfully complete 3/3 exercises.

So, Looping triangle;

document.write("<p>Excersise 1 - Looping triangle:<br />");
    for(let pyramid="#";pyramid.length<7;pyramid+="#"){
      document.write(pyramid);
      document.write("<br />");
    }
    document.write("</p>");

FizzBuzz;

    document.write("<p>Excersise 2 - FizzBuzz:<br /");
    for (let number=0;number<=100;number++){
      if(number%3 == 0 && number%5 == 0) document.write(number + " - FizzBuzz - Divisible by 3 and 5");
      else if(number%3 == 0) document.write(number + " - Fizz - Divisible by 3");
      else if(number%5 == 0) document.write(number + " - Buzz - Divisible by 5");
      else document.write(number);
      document.write("<br />");
    }
    document.write("</p>");

and finally Chessboard. I tried to fancy it up a little and do some extra steps to learn and experiment. By looking at oher examples, I see I could have done it much cleaner. The code;

  document.write("<p>Excersise 3 - Chessboard:<br />");
    let boardLenght=Number(prompt("We're creating a square chessboard. Enter the desired board length - number of fields per side (e.g. enter 8 for board size 8x8)."));
    if ((boardLenght > 0) && (boardLenght % 1 == 0) && (boardLenght != NaN)) {
      let whiteField="&nbsp";
      let blackField="#";
      for (let rows=1;rows<=boardLenght;rows++){
        if(rows%2 == 0) {console.log(rows +" = even row")} else {console.log(rows +" = odd row")};
        if(rows%2 == 0) // if it is an odd row, it should start with a black field
          for (let evenRowColumn = 1;  evenRowColumn <= boardLenght ; evenRowColumn++) {
            console.log("Even row executed: row " + rows + ", column " + evenRowColumn);
            if(evenRowColumn%2 == 0) {document.write(whiteField); console.log("    White field")}
            else {document.write(blackField); console.log("    Black field")};
            }
        else // if it is an even row, it should start with a white field
          for (let oddRowColumn = 1;  oddRowColumn <= boardLenght ; oddRowColumn++) {
            console.log("Odd row executed: row " + rows + ", column " + oddRowColumn);
            if(oddRowColumn%2 == 0) {document.write(blackField); console.log("    Black field");}
            else {document.write(whiteField); console.log("    White field");};
        }
        document.write("<br />");
      };
    }
    else alert("The number you have entered either isn't a positive number, isn't a whole number or is not a number at all. To try again, please reload the page.");
document.write("</p>");

Thank you.
J.


#331

My version of the 3 Exercises:

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

FizzBuzz

  var Fizz;
  var Buzz;
  for (var counter = 1; counter < 100; counter++){
    Fizz = counter % 3;
    Buzz = counter % 5;

    if (Fizz == 0 && Buzz == 0){
      console.log("FizzBuzz!");
    } else if (Fizz == 0){
      console.log("Fizz!");
    } else if (Buzz == 0){
      console.log("Buzz!");
    } else {
      console.log(counter);
    }
  }

Chessboard

  var chessboard = " ";
  var scale = 8;
  for (var line = 0; line < scale; line++){
    var row = 0;
      while (row<scale){
        chessboard += "# ";
        row++;
      }
    chessboard += "\n";
    if (line%2 == 1){
      chessboard += " ";
    }
  }
  console.log(chessboard);

Probably not the shortest version, but at least I can follow my steps this way at any time :smiley:


#332

TRIANGLE

for (let row = “#”; row.length <8; row += “#”)
console.log (row);

FIZZBUZZ

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

CHESSBOARD

 let length = 8;

  let board = " ";

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

  console.log(board);

#333

ALL WORKING

Triangle Loop:

let charPound = "#";
for (let index = 0; index < 8; index++) {
    console.log(charPound.repeat(index));  
}

FizzBuzz

for (let numStart = 0; numStart<100; numStart++) {
    switch (true) {
        case ((numStart>0) && (numStart%3 == 0)):
            console.log("Fizz");
            break;

        case ((numStart>0) && (numStart%5 == 0)):
            console.log("Buzz");
            break;

        default:
            console.log(numStart);
    }    
}

Chess Board

let size = prompt(Number("How many rows?")), board = "# # # #";
for (let count = 0; count < size; count++) {
    if (count%2==0) {
        console.log(" " + board)
    } else {
        console.log(board);
        }
}

#334

Only problem is if you want an odd amount of lines (since you combined the two lines into a single string). Here is how I solved it, which allows for an even or odd amount.

let size = prompt(Number("How many rows?")), board = "# # # #";
for (let count = 0; count < size; count++) {
    if (count%2==0) {
        console.log(" " + board)
    } else {
        console.log(board);
        }
}

#335

FizzBuzz

  for (let number=1; number<=100; number++){
    if (number%3==0 && number%5!==0){
      console.log('fizz');
    }else if (number%5==0 && number%3!==0){
      console.log('Buzz');
    }else if(number%5==0 && number%3==0){
      console.log('fizzBuzz')
    } else {
      console.log(number);
    }
  }

Chess Board

    let displayValue="";
    let columnRow = 8;
    for (let y=0; y<columnRow; y++){
      for(let x=0; x<columnRow; x++) {
        displayValue += (x + y)%2==0 ? " " : "#";
    }
      displayValue += "\n";
    }
  console.log(displayValue);

#336

FizzBuzz

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

if (counter % 3 === 0 && counter % 5 === 0) {
  document.write ("FizzBuzz" + "<br>");
}
else if (counter % 5 === 0 && counter % 3 > 0) {
  document.write ("Buzz" + "<br>");
}
else if (counter % 3 === 0) {
  document.write ("Fizz" + "<br>");
}
else  {
  document.write (counter + "<br>");
}

}

Chessboard

let rowsNum = 8;

let output1 = " #".repeat(rowsNum);
let output2 = "# ".repeat(rowsNum);

for (var row = 1; row < rowsNum; row++) {

if (row % 2 == 0) {
console.log (output1);
}
else {
console.log (output2);
}
}


#337

FizzBuzz

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

Chessboard

var size = 8;
let line = “”;

for (let row = 1; row <= size; row++){
if (row % 2 == 0){
line = “#”;
}else{
line = " ";
}
while (line.length <= size){
if(line.charAt(line.length-1) == “#”){
line += " ";
}else{
line += “#”;
}
}
console.log(line);
}


#338

FizzBizz Solution

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

Chess Board Solution

var row = ‘# # # #’
for(var i = 0; i < 8; i++) {
if(i % 2 === 0)
console.log(’ ’ + row);
else
console.log(row + ’ ');

    console.log('\n');
  }

#339

1.Looping a triangle:
for (var raw = 0; raw < 7; raw++) {
var TextToDisplay="#";
for (var col = 0; col < raw; col++) {
TextToDisplay+="#";
}
console.log(TextToDisplay);
}

  1. FizzBuzz:
for (var n = 1; n <=100; n++) { if (((n%3)==0)&&((n%5)==0)) { console.log("FizzBuzz"); } else if ((n%3)==0) { console.log("Fizz"); } else if ((n%5)==0) { console.log("Buzz"); } else console.log(n); } 3. ChechMate: let size =8; var string = ''; for (var r = 0; r <size; r++) { for (var c = 0; c < size; c++) { if ((r+c)%2==0) { string+=' '; } else string+='#'; } string+='\n'; } alert(string); I first drew this in search for a more elegant solution than conditional execution: 01234567 0 # # # # 1 # # # # 2 # # # # 3 # # # # 4 # # # # 5 # # # # 6 # # # # 7 # # # #

#340

FizzBuzz solution

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

}

Took me almost an hour to get the Chess board working correctly with any number you enter, but here it is

var gridSize = Number(prompt(“How big you wanna go?”));
let line = “”;

for(let row = 0; row<gridSize; row++){
  if(row%2 ==0){
    line = " ";
  }
  else{
    line = "#"
  }
   while(line.length<(gridSize*2)){
     if(line.charAt(line.length-1) == "#"){
      line += " ";
    }else{
      line += "#";
    }
   }
    console.log(line);
}

#341

Hello, I was wondering if you would be able to explain to me how you use the } in JavaScript please? I understand the if and else stuff but where the } goes is preventing me from moving forward as I don’t understand where it should go? Many thanks


#342

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

}

FIZZBUZZ

function 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);
}
}
}
fizzBuzz();

CHESSBOARD LOOP

var rows = 4;
var cols = 4;
for(var i=1; i<=rows; i++){

console.log(" # ");

}
for(var j=1; j<=cols; j++){
console.log("# ");
}
for(var i=1; i<=rows; i++){

console.log(" # ");

}
for(var j=1; j<=cols; j++){
console.log("# ");
}