Great, @ArvidK!
Just one comment…
You need to remove your opening
and the final
Because its logging an additional 0
to the console after the final "Buzz"
for 100. Can you see why?
Great, @ArvidK!
Just one comment…
You need to remove your opening
and the final
Because its logging an additional 0
to the console after the final "Buzz"
for 100. Can you see why?
Hi @Pendar!
You can also use the following (like you did):
toPrint += "\n";
When the inner for
loop has finished its 8 iterations for each row, this line of code adds a line break to the end of each row of #
s (after the final #, or after the final white space, depending on which row it is). The for
loops build up a string for each row and before the outer for
loop begins its next iteration it adds a line break to the end of the row which has been built up by the inner for
loop. This is why "\n"
has quotes, because it’s added to a string as the last character.
It is what is known as a special character. There are several of them and they are written with a preceding \
The preceding backslash is called escape notation and it indicates that the next character should not be rendered as usual (here, it’s not an n
but a line break). Here’s a link to a list of special characters in the MDN web docs — you need to scroll down to the 5th section Escape Notation :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
Great!
This is the most concise solution. As there is only one statement in the function body, you are right that we can omit the curly brackets, but then it’s clearer to put the code all on one line:
for (let line = "#"; line.length < 8; line += "#") console.log(line);
Otherwise you need to add the curly brackets:
for (let line = "#"; line.length < 8; line += "#") {
console.log(line);
}
Yours isn’t wrong, and it executes perfectly well, I’m just pointing out what standard best practice is.
That’s perfectly ok to look at other people’s solutions, as long as you do it AFTER you have first done as much thinking and experimentation as you can yourself… which it sounds like you did.
To be honest, the most important part of the learning process is in that first stage of thinking, experimenting and wrestling with the concepts. Whether your code actually works or not, or whether it’s the model answer or not, is of secondary importance (even though that’s obviously nice too ) as long as you ALSO spend time looking at the model answers and at other people’s solutions here in the forum, and SPEND TIME looking at why they work (or why they don’t), and also understanding what the different alternatives are.
You’ll learn loads by using that method (but you need to invest the time).
I could only manage fizzbuzz
<script>
for(a = 0; a<= 100; a++)
if (a%(3 * 5) == 0) console.log(“FizzBuzz”);
else if (a%3 == 0) console.log(“Fizz”);
else if (a%5 == 0) console.log(“Buzz”);
else console.log(a);
</script>
Wow, this is tough. Tried my best went through the chapters a few times already.
Let # =“7”;
while (#>7)
console.log(“output”);
For var n=1;n<=100;n++{
if n%3==0) write(“Fizz”);
If n%5==0) write(“Buzz”);
If n%3&& n%5==0 write(“Fizz Buzz”);
Let size=8
Let board =“”;
For(let y=0;y<size;y++;{
x=0;x<size;x++){
if(y+x)%2==01{board+=“”;}
board+=“/n”
}
console.log(board);
Hi @Kraken!
It looks like you are missing a parenthesis after the if
condition. It should be:
if ((counter % 3 === 0) && (counter % 5 === 0))
You will avoid this kind of problem much more easily if you don’t use parentheses when they aren’t needed (as it avoids clutter):
if (counter % 3 === 0 && counter % 5 === 0)
Otherwise, your FizzBuzz code looks fine
Yes, please don’t post screen shots, because we can’t copy your code into a text editor to run it and check it.
Before you type in your code to post it here in the forum, click on the </> icon in the menu at the top of the text editor. That will give you 2 sets of 3 back ticks…
```
```
If you now input your code between these, you will end up with it nicely formatted. You will probably still need to organise it a bit more with the correct spacing and indentation etc.
Post the code, using the method I’ve described above, and we can check it and find out what the problem is.
Hi @Katoch!
Looking good
Unfortunately, your ChessBoard code throws an error. It’s two minor mistakes with a variable name. Can you see where and what the problem is? I hope it’s not because you just copy-and-pasted a mixture of other people’s code, and that you did actually run it yourself to check that it worked…
Top tip: always check your code after you’ve posted it to the forum!
Hey @dani88!
That’s great that you’re going back over the material. Revisiting earlier topics, after you’ve gained some further knowledge, can often help you to see things differently and more clearly.
Please don’t get disheartened. If you were a complete beginner, then there is a huge amount to learn and take on board. And slow and steady often wins the race (in the end) Go at your own pace, and don’t compare yourself to how quickly others may seem to get through the material.
This is exactly right, and what makes programming as much an art as a science. It can be confusing if you wrongly think that you are always trying to get to a right answer. There often isn’t a right answer but instead an answer which solves the problem, or sometimes a different problem to the one you thought you were solving in the first place!
I know it can be really frustrating at times, but try to enjoy the challenge as much as possible. And remember, it’s often when you are wrestling with something the most, that you are learning the most (even though it may not seem like it at the time)!
Your scalable Chessboard solution is great! Well done
By the way, I’m not sure if you know, but before you type in your code to post it here in the forum, click on the </> icon in the menu at the top of the text editor. That will give you 2 sets of 3 back ticks…
```
```
If you now input your code between these, you will end up with it nicely formatted. You will probably still need to organise it a bit more with the correct spacing and indentation etc., but it’s much easier to do that effectively once it’s formatted than when it’s not. Your latest Chessboard solution should end up looking something this this:
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);
Thank you @jon_m for point it out. I labeled chessboard once I realized board was a vague term imo, but I didn’t update the rest of the code. I am so embarrassed
Hey!
Don’t worry… we all do it!
At least by embarrassing you, it will make you double check everything
1.FizzBuzz
for (var counter=1; counter<=100;counter++){
if (counter%3==0 &&counter%5==0){
console.log(“FizzBuzz”)
}
else if (counter%5==0){
console.log(“Buzz”)
}
else if (counter%3==0) {
console.log(“Fizz”)
}
else {
console.log(counter)
}
}
2. ChessBoard
var row_num = 3;
var colum_num = 8;
var simbol1="# “;
var simbol2=” #";
for (var colum =0; colum<colum_num; colum++){
if (colum%2==0){
var toPrint=simbol2;
for (var row=0; row<row_num; row++){
toPrint+=simbol2
}
}
else {
var toPrint=simbol1;
for (var row=0; row<row_num; row++){
toPrint+=simbol1
}
}
console.log(toPrint)
}
//FizzBuzz
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);
}
//Chessboard
var size = 8;
var string = "";
for(var rowCount=1; rowCount<=size;rowCount++)
{
for (var count=1; count<=size;count++)
{
if( (count + rowCount) % 2 != 0)
string = string + " ";
else
string = string + "#";
}
string = string + "\n";
}
console.log(string);
Looping a Triangle
let hash = " ";
while (hash.length <= 7){
hash += "#";
console.log(hash);
}
Fizz Buzz
for(let number = 0; number < 100; number++) {
if (number === 0)
console.log(number);
else if(number % 15 === 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
This one I struggled with and had the right idea, but kept going wrong. I ended up needing to compare my program with others to get it right.
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);
So here are my solutions to this exercises. At first I didn’t understand almost anything since I am very new to the whole concept of programming , so I started this course of JS programming again from beginning. It has deepen my understanding and now I sense I can move on with the course . When I didn’t understand why I was doing what it really did help alot to check around on the forum and on the internet what should I be doing. So thanks to the www and you guys I increased my understanding of JS. The last exercise was kind of the most difficult for me to understand so I used his version of exercise but I think if I keep going and doing… I will get more understanding and I will get better at it
So this is my solution to the Triangle Loop :
var a = 0, b = “#”, c = b;
while ( a < 7 ) {
if (a === 0 ) { console.log ( c ) }
else { console.log ( b+=c); }
a++;
}
FizzBuzz Loop
for ( 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)};
}
ChessBoard Loop
let size =8;
let border = “”;
for ( row = 0; row < size ; row ++ ) {
for ( collumn = 0; collumn < size ; collumn ++) {
if ((row + collumn) % 2 === 0 ) { border += " "}
else {border += "#";}
}
border += “\n”
}
console.log ( border )
Hey @realsloth!
Nice solution!
That’s fine! If Chessboard is too much to handle at the moment, then I would suggest these possible approaches (depending on what you’ve already tried yourself)…
Hi @Wing2020,
You’ve got the right idea for FizzBuzz and Chessboard, but did you try running your code? Because what you’ve posted doesn’t execute. Let’s use FizzBuzz as an example, and then you can try modifying Chessboard, because lots of the errors are the same or similar in both exercises.
You need to make the following amendments to your FizzBuzz code for it to execute successfully:
for
and if
;for
loop expressions, and also around if
and if...else
conditions;document.write
but it’s easier to use console.log();
document.write
;if
statement should be for “FizzBuzz”, otherwise “FizzBuzz” never gets logged, only “Fizz”.(n %
3 == 0
&& n % 5 == 0)
else
statement, which means numbers not divisible by 3 or 5 aren’t logged.for
statement’s closing curly bracket!I think that some of the minor errors are simply because you haven’t formatted your code. I’m not sure if you know, but before you type in your code to post it here in the forum, click on the </> icon in the menu at the top of the text editor. That will give you 2 sets of 3 back ticks…
```
```
If you now input your code between these, you will end up with it nicely formatted. This then also makes it easier for you to spot errors, and to organise your code with the correct spacing and indentation etc.
Your solution should look like this:
for (var n = 1; n <= 100; n++) {
if (n % 3 == 0 && n % 5 == 0) console.log("Fizz Buzz");
else if (n % 3 == 0) console.log("Fizz");
else if (n % 5 == 0) console.log("Buzz");
else console.log(n);
}
// or
for (var n = 1; n <= 100; n++) {
if (n % 3 == 0 && n % 5 == 0) document.write("Fizz Buzz<br>");
else if (n % 3 == 0) document.write("Fizz<br>");
else if (n % 5 == 0) document.write("Buzz<br>");
else document.write(n + "<br>");
}
Review this carefully, and then use what you’ve learnt to have another go at Chessboard. Make sure you run your code to check if it’s working. The error messages will tell you which lines are causing the problem.
I’m really not sure what happened to your Looping a Triangle. Rewatch the video where Ivan gives you a demonstration; look at the exercise in the course book again; look at the hints (which you can display in the online course book by clicking below the exercise instructions); or look at what other students have posted in this thread.
Good luck! You can do it!
Excellent, @Matoshi!
I can see you’ve worked really hard at this, especially Chessboard. Your solution is certainly different, in fact quite unique — but it works fine.
I actually think you’ve got your variable names for rows and columns mixed up. Have a look and see if you agree why I think it would be better to swap them. I think it would make it clearer to other developers and make your code more understandable.
The other modification I would suggest, is to put var row_num
under var colum_num
and then change var row_num
from:
var row_num = 3;
// to
var row_num = (colum_num/2)-1;
This then makes the only input variable for the user the real dimension i.e. 8 for 8x8 , and avoids them having to understand why there are only 3 rows (or columns if you swap the names) because now that’s just a calculation performed within the program. The main advantage of this, is that the user can now input any dimension (e.g. 10 for 10x10, or 16 for 16x16 etc.) and it outputs the corresponding chessboard correctly every time.
I hope that makes sense, and gets you thinking a bit deeper. Let me know if not…
Keep going! You’re doing great!
Hey, @HotCrytoGuy45!
It’s looking good!
Just one observation… your FizzBuzz is only outputting Fizz and Buzz, with no FizzBuzz for numbers divisible by both 3 and 5. So you need to add in an extra if
statement to handle that. I’ll leave you to think about where it should go, and how to code the condition.
Woooowwww that’s al lot of info.
I really struggled with this. I finally went to the forum (which was very helpful) to see if others were having the same issues.
I have read, then re-read, then read again and still struggling. I am now starting chapter 4 tonight but to tell you the truth, chapter 3 was a blur. Not sure if programming is for me. I just want to learn all I can (from everything). I have completed the bitcoin, Ethereum and this was next in line.
I will get through it. I will go over all the great info you gave me.
Thank you,
Nancy Winger