Chapter 2 Exercises


#309

Don’t Give up on me yet. I’m going to win this battle.


#310

this is my code for the hash triangle

var render = "#" , render1 = "#" , counter = 0;

  while (counter<7)
    	{
        document.write(render+"<br>");
        counter++;
        render=render+render1;
      }

#311

And here is the Fizzbuzz excercise

  var f = "fizz" , b = "buzz" , nf = 3 , nb = 5;

  for (var i = 1; i <= 100; i++)
    {
      if ((i%nb==0)&&(i%nf==0))
      {
        document.write(f + b + "<br>");
      }
      else if (i%nb==0)
      {
        document.write(b + "<br>");
      }

      else if (i%nf==0)
      {
        document.write(f + "<br>");
      }
      else {
      document.write(i + "<br>");
      }
    }

#312

And here is my solution for the chess board project.
My son helped me out with this so he gets some credit as well

var a = 8
var b = a+1

for (var i = 1; i < a*a; i++)
{
  if (i % b == 0)
  {
      document.write("<br>");
  }
  else if (i % 2 == 0)
  {
      document.write("#");
  }
  else
  {
      document.write("_");
  }
}

#314

Hello Community,

I solved the quests as follows:

Looping a triangle

for (var i = 0, max = 10; i < max; i++) {
    document.write("#");
        for (var j = 0; j < i; j++) {
            document.write("#");
        }
        document.write("<br>");
}

FizzBuzz

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

ChessBoard

var myTrigger = true;
var maxRow = 8;
var maxColumn = maxRow / 2;

for (var row = 0; row < maxRow; row++) {
    for (var column = 0; column < maxColumn; column++) {
        if (myTrigger === true) {
            document.write("\xa0\xa0");
            document.write("#");
        } else {
            document.write("#");
            document.write("\xa0\xa0");
        }
    }
    document.write("<br>");
    myTrigger = !myTrigger;
}

#315

My solution for the FizzBuzz exercise:

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

}


#316

My solution for the chessboard exercise:

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


#317

Looping a Triangle

for (let linetrack = "#"; linetrack.length <= 7; linetrack += "#"){
     console.log(linetrack);
}

FizzBuzz

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

Chessboard

let size = 8;

for (let rowCount = 1; rowCount <= size; rowCount += 1) {
	if (rowCount % 2 == 1) {
		let rowPattern = "";
		for (let columnCount = 1; columnCount <= size; columnCount += 1) {
			if (columnCount % 2 == 1) {
				rowPattern = rowPattern + " ";
			}
			else {
				rowPattern += rowPattern + "#";
			}
		}
		console.log(rowPattern);
	}
	else {
		let rowPattern = "";
		for (let columnCount = 1; columnCount <= size; columnCount += 1) {
			if (columnCount % 2 == 1) {
				rowPattern = rowPattern + "#";
			}
			else {
				rowPattern += rowPattern + " ";
			}
		}
		console.log(rowPattern);
	}
}

#318

Ivan these exercise are a little too much for complete beginners, I was just barely able to complete before giving up lol. The ‘%’ was messing me up because even though I read it, I couldn’t quiet understand it.

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

for(var num = 1; num<=100; num++){
toPrint ="";
if(num % 3 ==0) toPrint +=“Fizz”
else if(num % 5 ==0) toPrint +=“Buzz”
console.log(toPrint || num);
}

CROSSBOARD

var size = 8;

var board = “”;

for (var row = 0; row < size; row++) {
for (var column = 0; column < size; column++) {
if ((column + row) % 2 == 0) {
board += " ";
} else {
board += “#”;
}
}
board += “\n”;
}
console.log(board);


#319
<script>

// /Exercise 2 //

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

</script>

<script>

//Exercise 3 //

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

</script>

#320

My solutions are:
1- first exercise:

var linesPrinted=1;
var hashesPrinted=0;
while(linesPrinted < 10)
{

do {
document.write("#");
hashesPrinted++;
} while (hashesPrinted!=linesPrinted)

document.write("");
hashesPrinted=0;
linesPrinted++;
}

2- exercise
var totalNumber=100;
for(var count=0; count < totalNumber; count++)
{

if(count%3==0)
{

  document.write(" number is "+count+" Fizz");
  document.write("</br>");
  if(count%5==0)
  {
    document.write(" number is "+count+" FizzBuzz");
    document.write("</br>");
  }
}
else if (count%5==0)
{
    document.write(" number is "+count+" Buzz");
    document.write("</br>");
    if(count%3==0)
    {
      document.write(" number is "+count+" FizzBuzz");
      document.write("</br>");
    }
}

}

3- exercise is

var hash="#",space="&nbsp",toPrint="";
var change=true;
var size= prompt("enter the size of the chessboard: “);
for(var count; count < size; count++)
{
for(column=0;column<size;column++)
{
if(change==true)
{
//document.write(” inside true ");
if(column%2==0)
{
toPrint+=hash;
}
else
{
toPrint+=space;
}
}

  if(change==false)
  {
    //document.write(" inside false ");
    if(column%2==0)
    {
      toPrint+=space;
    }
    else
    {
      toPrint+=hash;
    }
  }
  if (column==size-1 && change == true)
  {
    document.write(toPrint);
    document.write("</br>");
    toPrint="";
    change=false;
  }
  else if (column==size-1 && change == false)
  {
    document.write(toPrint);
    document.write("</br>");
    toPrint="";
    change=true;
  }

}

}


#321

Fizzbuzz:
<script>

//write exercise here, Fizzbuzz

for (var i = 0; i < 101; i++) {

if (i%3==0 && i%5==!0) {

console.log("Fizz");

}

else if (i%5==0 && i%3==!0) { console.log("Buzz"); }

else if (i%5==0 && i%3==0) { console.log("FizzBuzz"); }

else {

console.log(i);

}

}

</script>


#322

Triangle Loop

for (let count = 0; count< 8; count++) { console.log(count); }

Fizz Bizz

for (var n=1; var n<= 100; n=++;)
{
var output = “”;
if (n % 3 == 0)
output += “Fizz”;

   if (n % 5 == 0)
  output +=  "Buzz";

console.log (output || n);
}

Stuck on number 3 trying to understand it and the answers that are given.


#323

Fizz Buzz loop
for(var i = 1; i<=100; i++){

if(i % 3 == 0 && i % 5 == 0){

console.log("FizzBuzz");

}

else if (i % 5 == 0) {

console.log("Buzz");

}

else if(i % 3 == 0){

console.log("Fizz");

}

else {

console.log(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 += "\n";

}

console.log(board);


#324

Triangle loop using repeat:

let rows = 7;
for (hash = 0; hash < rows; hash++){
console.log("#".repeat(hash+1))
}


#325

EXERCISE 1
//write exercise code
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);
}

EXERCISE 2
//write exercise code
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);
}

}

EXERCISE 3
//write exercise code
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);

Here’s the thing. I guess I still don’t quite get the idea of loops. Like I was trying to make a program where I could just make simple grid of symbols, where number of rows = number of columns. Does this require a loop too? I know I sound dumb but I’m trying to grasp this. I do understand the idea of the “if else” thing and how like in EXERCISE 2 we had to use that logic stream in order to prevent the numbers we were replacing from reappearing, but I don’t quite get the idea of variables and loops controlling each other. Ooph.


#326

Exercise 3a

let rowsw = 4;
for (line = 0; line < rowsw; line++){
console.log(" # # # #\n# # # # ")
}

I think this works as Chrome returns a result of 4 but doesnt display the actual lines themselves.


#327

var q_rows =“7”;
for( row=0; row<q_rows; row++){
var sharp="#";
for( col=0; col<row; col++){
sharp+="#";
}
document.write("

"+ sharp +"

")
}

var hundred= “100”;
for( var counter =1; counter <=hundred; counter++){
var three = counter % 3 == 0;
var five = counter % 5 == 0;
if( five && three ){
document.write(“

FIZZBUZZ

”)
}
else document.write("

"+ counter +"

")
}

var place=“8”;
var space=" “;
for( x=0; x<place; x++){
for( y=0; y<place; y++){
if((x+y) % 2==0)
space+=” “;
else
space+=”#"
}
space+="
";
}
document.write("

"+ space +"

")

#328

My FizzBuzz program
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)

}


#329

Here are my JavaScript Chapter 2 exercises:

Triangle

var toPrint = "";
while (toPrint.length < 7) {
   toPrint += '#';
   document.write(toPrint + "<br>");
}

Triangle (shorter version)

for(var toPrint = '#'; toPrint.length <= 7; toPrint += '#') {
   document.write(toPrint + "<br>");
}

FizzBuzz

for(var i = 1; i <= 100; ++i) {
  var out = "";
  if (i % 3 == 0) out += 'Fizz';
  if (i % 5 == 0) out += 'Buzz';
  document.write((out||i)+"&nbsp;&nbsp;&nbsp;");

  // breaks a big line into smaller-size ones
  if (i % 10 == 0) document.write("<br>");
}

Chessboard

var size = 8;
var out = '';

// creates an object of length (size-1)
while( out.length < size - 1 ) out += (out.length % 2 ? ' ' : '#');

// print that object size times
for(var i = 0; i < size; ++i)  {
   if(i%2) document.write("|"+out+" |<br>");
   else    document.write("|&nbsp;"+out+"|<br>");
}