Looping a Triangle
FizzBuzz
ChessBoard
Finally figured this out… not sure why this one was a struggle for me.
let size = 8
let board = "";
for(let row = 0; row < size; row++){
for(let column = 0; column < size; column++){
if ((row + column) % 2 == 0)
board += " ";
else
board += "#";
}
board += "\n";
}
Un plaer @BohemianJC!
Glad it was helpful.
Good luck with those Chapter 3 exercises! Molta força
Hi @RLion, just some correction in Fizzbuzz. On your last condition, Instead of OR(||) you should use AND(&&) because both condition have to be true. correct version of your function would be this.
for(var i =0; i <= 100; i++){
if((i % 3 == 0)&&(i % 5 == 0)){
console.log(i + "fizz buzz");
}
else if(i % 5 == 0){
console.log(i + "buzz");
}
else if(i % 3 == 0){
console.log(i + "fizz");
}
}
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);
}
FIZZ BUZZ
for(var number = 0; 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
for (var row = 1; row <= 8; row++){
var toPrint = "";
for(var col=1;col<=8; col++){
if(row%2==0){
if(col%2==0){
toPrint += " ";
}
else (toPrint +="#");
}
else if (col%2==0){
toPrint += "#";
}
else(toPrint += " ");
}
console.log(toPrint);
}
Thanks, it is clear.
// Triangle Loop exercise code
var number_rows = 7;
for (var row = 0; row < number_rows; row++)
{
var toprint = "#";
for(var column = 0; column < row; column= column +1)
{toprint += "#";
}
console.log(toprint);
}
// FizzBuzz exercise code
for (var row = 1; row < 101; row++)
{
if(row%3==0 && row%5== 0) toprint="BuzzFizz";
else if(row%3==0) toprint="Fizz";
else if(row%5== 0) toprint="Buzz";
else toprint = row;
console.log(toprint);
}
// Chess Board exercise code
var height = 8
var width = 8
var board1 = " #"
var board2 = "# "
for (var row = 0; row < height; row++)
{
if (row%2==0) toprint = board1.repeat(width/2);
else toprint = board2.repeat(width/2);
console.log(toprint);
}
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);
}
FIZZBUZZ
for (let n = 1; n <= 100; n++) {
let output = "";
if (n % 3 == 0) output += "Fizz";
if (n % 5 == 0) output += "Buzz";
if (n % 3 && 5 == 0) output += "Fizzbuzz";
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);
Thank you so much @jon_m !! I really needed to hear that as I was really starting to doubt myself Your post was extremely helpful and even though for the most part I’ve worked out these exercises, I’m going to read through the posts you linked to deepen my understanding.
Thanks again!
Shalom Danny! your example of fizz buzz help out great…thanks…i was just missing a few things
//Checkerboard Exercise
/*var num_rows = 8
for (var row = 1; row <= num_rows; row++){
if (row % 2 == 0) var toPrint = “# # # #”;
else toPrint = " # # # #";
console.log(toPrint);
}*/
//Fizzbuzz
/for (var 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);
}/
// Looping triangle exercise
var rowsNum = 7;
for( var row = 0; row < rowsNum; row++){
var toPrint = "#";
for(var column = 0; column < row; column++){
toPrint += "#";
}
console.log(toPrint);
}
// FizzBuzz exercise
for(counter = 1; counter <= 100; counter++){
if(counter % 3 == 0){
if(counter % 5 == 0){
console.log("FizzBuzz");
} else{
console.log("Fizz");
}
}
else if(counter % 5 == 0){
console.log("Buzz");
} else{
console.log(counter);
}
}
// Chessboard exercise
var size = 8;
var marker = "";
for(var x = 0; x < size; x++){
for(var y = 0; y < size; y++){
if((x+y) % 2 == 0){
marker += " ";
} else{
marker += "#";
}
}
marker += "\n";
}
console.log(marker);
TRIANGLE
var numrow = 7
for(var row = 0; row < numrow; row++){
var print = "$"
for(var colu = 0; colu < row; colu++){
print = print + "$"
}
console.log(print)
}
BUZZ
for(i=1; i<=100; i++){
if (i % 3 == 0 && i % 5 == 0){
console.log("trecinque")
}
else if (i % 3 == 0){
console.log("tre")
}
else if (i % 5 == 0){
console.log("cinque")
}
else{
console.log(i)
}
}
CHESS
var n = 10
var L = " #"
var L1 = "# "
for(var y = 1; y < n; y++){
L = L + " #"
L1 = L1 + "# "
}
for(var x = 0; x < n ; x++){
if (x % 2 == 0) {
console.log(" ")
console.log(L);
} else{
console.log(" ")
console.log(L1);
}
}
for (var x = “#”; x.lenght < 8; x += “#”) console.log(x);
for (var x = “#”; x.length < 8; x += “#”) console.log(x);
THE FIRST DOESN’T WORK…i can’t understand why since it looks identical to the second one. Somebody maybe see the mistake? thanks
Hey enrico,
I guess you misspelt the word “length”.
Happy learning.
thanks so much…I spent an afternoon with this
I did mine a bit differently, making triangle it’s own function and writing to document instead of to console. Here’s what I did:
function triangle(size){
for (let hash = "#"; hash.length <= size; hash += "#"){
document.write(hash);
document.write("</br>")
};
};
triangle(7);
made a chessboard() function. My code is a little different than the solution:
<script>
function chessboard(size){
board = "";
let evenOrOdd = 0;
/* this binding will help determine the odd or even conditional start for each
new chessboard line */
for (y = 0; y < size; y++){
for(x = 0; x < size; x++){
if ((x + evenOrOdd) % 2 == 0){
/* so instead of (x + y) to determine it's even-ness (as EJ's way more
elegant solution shows), it adds the evenOrOdd variable which adds one
increment after the "\n" string character is added */
board += " ";
} else {
board += "#";
}
}
board += "\n"
evenOrOdd++
};
console.log(board);
};
/* so because I created a chessboard() function, we can now size our board
to whatever size we want. */
chessboard(8);
</script>
CHESS EXERCISE
I did it in a different way than the solution. What does this line means?
board += “\n”;
what’s “\n” ? thanks
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);