Chapter 2 Exercises

Hi @JasonB

Even it’s less ‘compact’ and ‘effective’ as you said it’s always more rewarding to do everything by yourself to really understand what happen under the hood.
Your code is working well and this is the most important, looking at other students answers after having completed yours is also a good things to learn new tricks.

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

ChessBoard
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)
1 Like

01.)Looping a triangle

var hashTags = "#######";
for (i = 1; i <= hashTags.length; i++) {
	var row = hashTags.substring(0,i);
	console.log(row);
}

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

03.) Chessboard

var hashBlank ="# ";
var blankHash =" #";
var boardSize;

do {
boardSize = Number(prompt("Chessboard will be B x B. Choose number for B"));
} while (Number.isNaN(boardSize));
if (!Number.isNaN(boardSize)) {alert("Size of your chessboard will be " +boardSize+ " x " +boardSize)}

for (i = 1; i<=boardSize; i++){
var chessboard = "";
if (i%2!=0){
		for (j = 1; j<=(boardSize/2); j++){
			chessboard = chessboard + blankHash;
			}
	}
else {
		for (k = 1; k<=(boardSize/2); k++){
			chessboard = chessboard + hashBlank;
			}	
	}
console.log(chessboard);
}
1 Like

I must be totally useless because after 2 days (around maybe 10 hours altogether) of messing around, I had to simply copy and paste an answer from here in order to be able to enact the code in the console developer. That was just on the ‘FizzBuzz’ one!?!

Even after seeing the answers then trying to memorise the code and recreating it, I still couldn’t do it; left a pointy bracket out, added something that wasn’t necessary, not knowing whether the semi-colon should be there or not…

No need for any encouragement - just getting it off my chest as to how frustrating this is.

I don’t give up easily!!

Don’t care if this takes up loads of room on here - I did it.

1 Like
  1. for (let i = “#”; i.length <= 7; i += “#”)
    console.log (i)

  2. let n = 1;
    while(n < 101){
    if(n % 3 == 0 && n % 5 !== 0){
    console.log(“Fizz”);
    }
    else if (n % 3 !== 0 && n % 5 == 0) {
    console.log(“Buzz”);
    }
    else if (n % 3 == 0 && n % 5 == 0) {
    console.log(“FizzBuzz”);
    }
    else {
    console.log(n);
    }
    n++;
    }

  3. let boardSize = 8;
    let board = “”;
    for (let i = 0; i < boardSize; i++) {
    for (let j = 0; j < boardSize; j++) {
    if (i % 2 == 1) {
    if (j % 2 == 0) {
    board += “#”;
    } else {
    board += " ";
    }
    } else {
    if (j % 2 == 1) {
    board += “#”;
    } else {
    board += " ";
    }
    }
    }
    board += “\n”;
    }
    console.log(board);

1 Like

:zipper_mouth_face:
Just get it off your chest, that helps! :+1:

After feeling that I had achieved something by completing the chessboard exercise, I then realised that I needed to have the option to alter the size of the board grid within the code :sob: (which doesn’t work with my original code).

Even after studying one of IOT’s student’s successfully submitted code and attempting to recreate it, I have to admit, I couldn’t.

Also, after copying and pasting some of the completed (supposedly successful) in the postings here into my dev console, I found that quite a few don’t actually work - especially when changing the grid size to an odd number.

Is anyone checking this code?

I’d be surprised if 10% of beginners managed this task without looking at the solution in the book or at the solutions posted.

Is it just me?

Need to get over this chess board assignment and move on, I think. It’s taken up so much of my time.

1 Like

Hi.

We usually manage to look at code and see whether it is correct or not, and often we do not comment on the fact that there are small errors when using characters like "", which clearly comes from the fact that it was posted without the use of formatted text feature.

I would say that it is not so smart to just cut and paste from others, but rather enter the code itself, and understand what it does. Then it will be easier to find the bug, if there are any. :wink:

I’ve double-checked the last scripts posted and they work as expected. I just needed to change from
(“) to (") in one of the scripts. And that I think is a trifle and I don’t want to correct that.

Ivo

1 Like

Can someone please tell me what is wrong with this code for the chessboard…

Time to move on.

Hi.
The first thing I notice is that you are missing a couple of ;
Can you see where?
I don’t want to solve it for you when you are so close…
ivo

Hey, no it is not just you - I am trying to figure it out for days (only if I have time) and now looking here for some inspiration.
But still would like to do it my way - even if it is not “well written code” :slight_smile:
So you are not alone.

1 Like

Good to know, eh. Thanks.

As a student of languages myself, I guess learning a computer language has many similarities.

There are many rules that need to be abided by to make sense to the one (or thing) we are communicating with.

Keep on keeping on…These things take time and effort. Like anything in life, if it was too easy, it wouldn’t be worth doing!! :man_juggling:

2 Likes

Hi everyone!
I have done all the exercises on my test www page instead of console so there are some differences. I did it all by myself and I got shocked when I saw the book’s author answers later :open_mouth: , especially on 3rd exercise. This is what I’ve done:

  1. Triangle:
<script>
          document.write("<p>1.Triangle:</p>");
          let znak = "#";
          for (let count=0; count<=6; count++){
            document.write("<div>" + znak + "</div>");
            znak = znak + "#";
          }
        </script>
  1. Fizz and Buzz:
<script>
          document.write("<p>2.Numbers, except numbers that are divided by 3 or/and 5:</p>");
          for (let number=1; number<=100; number++){
            if ((number % 3 == 0) && (number % 5 == 0)){
              document.write("<div>FizzBuzz</div>");
            }
            else if (number % 3 == 0){
              document.write("<div>Fizz</div>");
            }
            else if (number % 5 == 0){
              document.write("<div>Buzz</div>");
            }
            else document.write("<div>"+number+"</div>");
          }
        </script>
  1. Chessboard:
    Please let me know what do You think about this, especially when it comes to manage the hollow space in a string. I used “_” because my program doesn’t menage the blank space.
  <script>
          let width = 8;
          let height = 8;
          let string1 = "_#";
          let string2 = "#_";
          document.write("<p>3.Chessboard "+width+"x"+height+" :</p>");
          while (width >= 2){
            string1 = string1 + "_#";
            string2 = string2 + "#_";
            width = width - 1;
          }
          while (height >= 2){
            if (height % 2 == 0){
            document.write("<div>"+string1+"</div>");
            }
            else document.write("<div>"+string2+"</div>");
            height = height - 1;
          }

        </script>
1 Like

My solutions (all in one file, output to HTML):

	document.write("<h3>1. Triangle:</h3>");
		
	for (t=0; t<=7; t++) {
		var hashes = "";
		for (tt=0; tt<t; tt++) {
			hashes += "#";
		}
		//hashes += "\n";
		document.write("<p>"+hashes+"</p>");
	}
	
	document.write("<br />");
	document.write("<h3>2. FizzBuzz:</h3>");
	
	for (f=1; f<=100; f++) {
		if (((f%3) == 0) && ((f%5) != 0)) {
			document.write("<p>Fizz</p>");
		} else if (((f%5) == 0) && ((f%3) != 0)) {
			document.write("<p>Buzz</p>");
		} else if (((f%5) == 0) && ((f%3) == 0)) {
			document.write("<p>FizzBuzz</p>");
		} else {
			document.write("<p>"+f+"</p>");
		}
	}
	
	document.write("<br />");
	document.write("<h3>3. Chessboard:</h3>");
	
	var size = 8;
	var currentChar = "#";
	
	for (v=1; v<=size; v++) {
		var row = "";
		if (currentChar == "#") {
			currentChar = "&nbsp;";
		} else {
			currentChar = "#";
		}
		for (h=1; h<=size; h++) {
			if (currentChar == "#") {
				currentChar = "&nbsp;";
			} else {
				currentChar = "#";
			}
			row += currentChar;
		}
		document.write("<p>"+row+"</p>");
	}	
	
	</script>
</body>
1 Like

Triangle loop:

            var numRows = 7;

			for (var row = 0; row < numRows; row++){
				var toPrint = '#';

				for(var column = 0; column < row; column++){
					toPrint += ' ';
				}
				console.log(toPrint);
				}

FizzBuzz:

	for (var i = 1; i <= 100; i++) {
			var FB = '';

				if(i % 3 == 0) {FB += 'Fizz'}
				if(i % 5 == 0) {FB += 'Buzz'}
				 
				
	
		    console.log(FB || i)	
		}

chessboard:

            var size = 8;
			var board = '';

			for(var h = 0; h < size; h++){
				for (var i = 0; i < size; i++){
				if (( i + h ) % 2 == 0) {
					//empty 
					board += ' ';
				} else { 
					//hashtag
					board += '#';
				}
				}
				board += '\n'
			}
			console.log(board);
1 Like
  1. Looping a triangle
    for(var row = “#”; row.length <= 7; row += “#”) {console.log(row);}
  2. FizzBuzz
    for (a = 1; a <= 100; a++) {
    if (a%3 == 0 && a%5 !== 0) console.log(“Fizz”);
    else if (a%5 == 0 && a%3 !== 0) console.log(“Buzz”);
    else if(a%3 == 0 && a%5 == 0) console.log(“Fizz” + “Buzz”);
    else console.log(a);
  3. Chessboard
    var square = “”;
    for (r = 0; r < 8; r++) { for (c = 0; c < 8; c++) {
    if ((r + c)%2 == 0) {square += " "}
    else {square += “#”;}
    }
    square+= “\n”;
    }
    console.log(square);
1 Like

Hi @nepo

Good job, you are just missing a closing curly bracket in your response 2. But i guess it’s a typo

//FizzBuzz

//the loop

for (var i = 1; i < 101; i++)
{
  //first check if its a fizzbuzz divisable by 5 and 3
  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);}
}

//not the prettiest but it does work

//Chessboard
//get the board size

var _boardSize;
var _boardString = "";
do { _boardSize = Number(prompt("Set board size greater than 1"));

} while (_boardSize < 2);
//print the board
for (var i = 0; i < _boardSize; i++) {
  //another loop for each row
  for (var i2 = 1; i2 <= _boardSize; i2++){
    //even is a #
    if (((i2 + i) % 2 == 0 )) {_boardString += "#";}
    else {_boardString += " ";}
  }
  //add the newline
  _boardString += "\n";
}
//print it out
console.log(_boardString);
1 Like

// Now makes complete sense, when you said, to code is hard… LOL… <3.

//Fizzbuzz

for (i = 0; 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)
			}
1 Like