Note: Chessboard, not yet completed. Still working on second half to include “else” and “\n”. The above counts out eight rows of chess hashes in JS sandbox. There is more to this exercise.
//FizzBuzz
var num_rows = 100;
for (row = 1; row <= num_rows; row++ ) {
if((row % 3 == 0) && (row % 5 !==0)) {
console.log("Fizz");
}
else if((row % 3 !== 0) && (row % 5 == 0)) {
console.log("Buzz");
}
else if ((row % 3 == 0) && (row % 5 == 0)) {
console.log("FizzBuzz");
}
else {
console.log("row number " + row);
}
}
//Chessboard
//change num_rows, num_cols for height and width
var num_rows =8;
var num_cols = 10;
var a = " “;
var b = " #”;
for (var row = 0; row < num_rows; row++){
if (row % 2 !== 0)
var toPrint = b;
else if (row % 2 ==0) {
toPrint = a;
}
for (var column = 0; column < num_cols; column++) {
if (column % 2 !== 0)
toPrint += b;
}
console.log(toPrint);
}
First let me say I’ve went back and forward many times, even cried about these, I managed to make them with help. I could not do them entirely on my own. I had to google many videos and advice.
TRIANGLE:
for(let triangle = “#”; triangle.length <= 7; triangle += “#”)
console.log(triangle);
FIZZBUZZ:
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);
}
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);
FizzBuzz code
<script>
for (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("Buzz")
}
else
{
if (number % 3===0)
{
console.log("Fizz")
}
else
{
console.log(number)
}
}
}
}
</script>
Chessboard code
<script>
let size = 8;
var unpair = " #";
var pair = "# ";
var string = ""
for (row=0; row<size; row+=2)
{
for (collumn=0; collumn<size; collumn+=2)
{
string=string+unpair
}
string = string +"\n"
for (collumn=0; collumn<size; collumn+=2)
{
string=string+pair
}
string= string + "\n"
}
console.log(string)
</script>
//Chessboard
var size = 8;
var board = "";
for (var column = 0; column < size; column++) {
for (var row = 0; row < size; row++) {
if ((column + row) % 2 === 0) {
board = board + " ";
} else {
board = board + "#";
}
} board = board + "\n"
}
console.log(board);
i used something similar for FizzBuzz, but it got caught in an infinite loop… i’m pretty sure i have some syntax off, but i reply to you here in order to ask you to confirm; did this code work??
i used:
for (n=1; n<=100; n+1) {
if (n % 3 != 0 && n % 5 != 0) {console.log (n)}
else if (n % 3 == 0 && n % 5 != 0) {console.log (“Fizz”)}
else if (n % 3 != 0 && n % 5 == 0) {console.log (“Buzz”)}
else {console.log (“FizzBuzz”)}
}
not even getting past
for (n=1; n<=100; n++) {
if (n % 3 != 0 && n % 5 != 0) console.log (n); n++;
}
… to add
elseif (n % 3 == “0” && n % 5 != “0”) console.log (“Fizz”); n++;
elseif (n % 3 != 0 && n % 5 == 0) console.log (“Buzz”); n++;
console.log (“FizzBuzz”); n++;
(which throws an “unexpected identifier” error) … only have so much time in my life to fail…
moving on…
similarly (i.e. i know the below is incorrect, but i don’t have much more time to waste on it) all my attempts centered around using the length of the string as an indicator for where to put line breaks,
e.g.
for (board = “”; board.prototype.length<=64 ; board++) {
if (board.prototype.length % 2 == 0) {board += " # "} ;
if (board.prototype.length % 8 == 0) {board += “\n”} ;
}
console.log(board)
…but somehow this wasn’t recognized even in the answer code; when inserting a console.log statement after board, e.g.
"
…
board += “#”;
console.log(board.prototype.length)
…
"
i got “TypeError: Cannot read property ‘length’ of undefined (line 11 in function eval)”
hopefully i haven’t lost out on too much by moving on…
//Chessboard to re-submit
var whiteRow = " # # # #"
var blackRow = "# # # # "
var whiteRowToBuild ="";
var blackRowToBuild ="";
for(let i = 0; i <8; i++){
if(i%2 == 0){
whiteRowToBuild += " ";
blackRowToBuild += “#”;
}
else{
whiteRowTouild += “#”;
blackRowToBuild += " ";
}
}
var chessboardToBuild = " “;
for(var row = 0; row 8; row++){
if(row%2 == 0){
chessboardToBuild += whiteRowToBuild =”\n"
}
}
console.log(chessboardToBuild);
<script>
//Chessboard(assisgnment and hints)
//for loop: count 0 through 7,
//define loop variable,
//condition to keep looping,
//and updates to build chessboard
var whiteRow = " # # # #"
var blackRow = "# # # # "
for(var row = 0; row<8; row++){
if(row%2 == 0){
console.log(whiteRow)
}
if(row%2 == 1){
console.log(blackRow)
}
}
var whiteRowToBuild ="";
var blackRowToBuild ="";
//var chessBoardToBuild ="";
//hint to increase size from 8
for(let i = 0; i<12; i++){
if(i%2 == 0){
whiteRowToBuild +=" ";
blackRowToBuild +="#";
}
else{
whiteRowToBuild +="#";
blackRowToBuild +=" ";
}
var chessboardToBuild ="";
for(var row = 0; row<12; row++){
if(row%2 == 0){
chessboardToBuild += whiteRowToBuild +"\n"
}
if(row%2 ==1){
chessboardToBuild += blackRowToBuild ="\n"
}
}
console.log(chessboardToBuild);
}
</script>
For some reason my exercise for Chapter 2 Chessboard did not copy and paste although it did copy and paste briefly in the JS Sandbox. After many adjustments and modifications, it did take and result in a chessboard. It did not here. It took me many hours to figure how to build the chessboard. Sorry it did not take but it was completed.
//Looping a Triangle - I used 1 loop
var num_rows = 7;
var toPrint = "#";
for (var row = 0; row < num_rows; row++) {
console.log(toPrint);
toPrint += "#";
}
//FizzBuzz - modulus used for checking conditions and nested if used for divisible by 3 and 5
var num_rows = 100;
var numToPrint = 1;
for (var row = 0; row < num_rows; row++) {
if (numToPrint % 3 === 0) {
if (numToPrint % 5 === 0) {
console.log("FizzBuzz");
}
else console.log("Fizz");
}
else if (numToPrint % 5 === 0) {
console.log("Buzz");
}
else console.log(numToPrint);
numToPrint++;
}
//Chessboard - modulus to check odd or even, nested for statement to build the row data
var num_cells = 10;
var rowToPrint = "";
for (var row = 0; row < num_cells; row++) {
if (row %2 == 0) {
for (var column = 0; column < num_cells; column++) {
if (column % 2 == 0) {
rowToPrint = rowToPrint + " ";
}
else {
rowToPrint = rowToPrint + "#";
}
}
console.log(rowToPrint);
rowToPrint = "";
}
else {
for (var column = 0; column < num_cells; column++) {
if (column % 2 == 0) {
rowToPrint = rowToPrint + "#";
}
else {
rowToPrint = rowToPrint + " ";
}
}
console.log(rowToPrint);
rowToPrint = "";
}
}
Chess Board
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
</script>
</head>
<body>
<script>
let board="";
for(x=0;x<=7;x++) {
for(y=0;y<=7;y++) {
if ((x+y)%2==0) {
board+=" "; }
else {
board+="X"; }
}
board+="\n";
}
console.log(board);
//
let board0="";
for(x=0;x<=7;x++) {
for(y=0;y<=7;y++) {
if ((x+y)%2==0) {
board0+=" "; }
else {
board0+="X"; }
}
board0+="<br>";
}
document.write(board0);
</script>
</body>
</html>
Looping Triangle
var hash = “”;
var counter = 0;
while (counter < 7) {
hash += “#”;
console.log(hash);
counter++;
}
// or if you use the hint in the book with “.length”
var hash = “”;
for (var counter = hash.length; hash.length<7; counter++){
hash += “#”;
console.log(hash);
}
FizzBuzz
for (counter = 1; counter <= 100; counter++){
let devision3 = (counter % 3);
let devision5 = (counter % 5);
if (devision3 === 0 && devision5 === 0){
console.log(“FizzBuzz”);}
else {
if (devision3 === 0){
console.log(“Fizz”);}
else {
if (devision5 === 0){
console.log(“Buzz”);}
else {
console.log(counter);
}
}
}
Chessboard
// first an inefficient way, but that was the first working one I came up with, since I struggled to print out the even and odd cells of the matrix within one string:
let size = 8
let even = “#”;
let odd = " ";
let text = “”;
if (size === 1) {
text = odd;
console.log(text);
} else if (size % 2 === 0) {
row1 = odd + even;
row2 = even + odd;
row1 = row1.repeat(size/2);
row2 = row2.repeat(size/2);
text = row1 + “\n” + row2 + “\n”;
text = text.repeat(size/2);
console.log(text);
} else {
row1 = odd + even;
row2 = even + odd;
row1 = row1.repeat(size/2);
row2 = row2.repeat(size/2);
text = row1 + odd + “\n” + row2 + even + “\n”;
text = text.repeat(size/2);
console.log(text);
}
//here the more efficient way:
let size2 = 8;
let board = “”;
for (r = 1; r <= size2; r++){
for (c = 1; c <= size2; c++){
let cell = (r+c)%2;
if (cell === 0) {
board += " “;}
else {
board += “#”;}
}
board +=”\n";
}
console.log(board);
The assignment asked that we make the chessboard a larger tabulation such as 12. I tried 9 and 10 and it had varyng lengths and widths of spaces and hashtags. Twelve (12) worked nicely though it was 12 strings long. To add extra sizes make sure the numbers match, i.e. 8 throughout and if switching to 12, then 12 throughout. I noticed that there was quite a bit of red underlining. I haven’t learned the colors yet but imagine that red is not a favorite. If I try this one again, I will work on removing the red underlining and find new keywords and symbols. I learned much with this assignment.
Fizzbuzz
<!doctype html>
FizzBuzz<script>
for(var count = 1; count <= 100; count ++) {
if(count % 3 == 0) {
console.log("Fizz");}
else if(count % 5 == 0){
console.log("Buzz");}
else {
console.log(count);}
}
</script>
Chessboard V.1 so first I solved this without help, and realized my result was different from everyone else’s but it works!! Lol, V.2 uses Loops.
<!doctype html>
Chessboard<script>
var size = 10;
var lengthAndWidth = size / 2;
var a = " " + "#"
var b = "#" + " ";
toPrint = a.repeat(lengthAndWidth) + "\n" + b.repeat(lengthAndWidth) + "\n";
console.log(toPrint.repeat(lengthAndWidth));
</script>
Chessboard V.2
This one uses loops and it’s quite concise. Needed help on the forum for this however.
<!doctype html>
<script>
var size = 8;
var board = “”;
for(var column = 0; column < size; column++) {
for(var row = 0; row < size; row++) {
if((row + column) % 2 === 0) {
board = board + " "; }
else {
board = board + “#”;
}
} board = board + “\n”
}
console.log(board);
</script>
Triangle
var numRows = 7
for (row = 0; row<numRows; row++){
var generateHash = “#”;
for(column = 0; column<row; column++){
generateHash += "#"
}
console.log(generateHash);
}
Fizzbuzz (disgustingly long compared to most other answers on here, but it works)
for(counter = 1; counter<=100; counter++){
var fizzCond = (counter %3 == 0);
var buzzCond = (counter %5 == 0);
var fizzBuzzCond = (fizzCond && buzzCond);
if(fizzCond && !buzzCond){
console.log("fizzz")
}
if ((buzzCond && !fizzCond) && (buzzCond && !fizzBuzzCond)){
console.log("buzzz")
}
if(fizzBuzzCond){
console.log("fizzzbuzzzzz")
}
if(!fizzCond && !buzzCond){
console.log(counter)
}
}
Chessboard -gave up and looked at the answer. Seemed so obvious when it was there in front of me…
Ugh I was so bummed out with my answers yesterday, I decided to do all of them again. It’s a little better, I think.
Triangle:
for(let line = "#"; line.length < 8; line += "#"){
console.log(line)
}
Fizzbuzz:
let num = 100
for(count = 0; count <= num; count++){
if( (count %3 == 0) && (count %5 == 0) && count !=0){
console.log("fizzbuzz")
}
else if(count %3 == 0 && count != 0){
console.log("fizz")
} else if((count %5 == 0) && (count %3 != 0) ) {
console.log("buzz")
}
else {
console.log(count)
}
}
Chessboard:
let size = 8;
let board = "";
for(rank = 0; rank < size; rank++){
for(file = 0; file < size; file++){
if((rank + file) %2 == 0){
board +=" ";
} else {
board +="#"
}
}
board +="\n"
}
console.log(board)
Looping triangle ‘#’:
var maxHash = "########";
for (var hash = "#"; hash.length < maxHash.length; hash += "#") {
console.log(hash);
}
FizzBuzz:
This is the closest I got to solving it… I looked at Google to get the “Number.isInteger” because I forgot about the remainder (%) operator…
Everything was calm with my code until I had to get the FizzBuzz… This is my final solution, after wracking my brain doing trial and error with the final “FizzBuzz” bit. I looked at the solution and some of the answers on the forum, but there’s no point in me copying the solution and posting, for the sake of “marking as complete” if I don’t understand the solution for myself… If anyone could let me know where I went wrong with this exercise, that would be amazing
for(var num = 1; num <= 100; num++) {
var t = (Number.isInteger(num / 3));
var f = (Number.isInteger(num / 5));
if(t && num) {
console.log("Fizz")
}
else if (f && num) {
console.log("Buzz")
}
if (t && f) {
console.log("FizzBuzz")
}
else {
console.log(num)
}
}
Cheesboard:
I won’t lie, I didn’t know how to approach this one…
Could someone help to explain what the solution means (line by line if possible)?
Here’s the solution from the eloquent JS website:
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);
#1
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
for(let number=1; number<=100; number++){
let output = "";
if (number % 3 == 0) output += "Fizz";
if (number % 5 ==0) output += "Buzz";
console.log(output || number);
}
#3
let size = 8; //sets a limit for interations to be used in our loop
let board = ""; // sets a blank placeholder to be filled with if statmements
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);
Overall I did have a hard time figuring out the questions and eventually had to see the answer. I try to watch videos that go step by step to really learn how the code is built.
I don’t understand anything.