You’re very welcome, Em!
I’m guessing you used  
and not &espn
Good luck with round 2!
You’re very welcome, Em!
I’m guessing you used  
and not &espn
Good luck with round 2!
Hi @Matgas,
Your code looks fine, but you need to post your actual code in order for us to be able to run it and find any bugs.
Please, don’t post screenshots. To format your code before posting it here in the forum, you can click on the </> icon in the menu at the top of this forum’s text editor. This will give you 2 sets of 3 back ticks.
```
input your code here
```
If you now input your code between these, you will end up with it nicely formatted, which is then also easier for you to organise with the correct spacing and indentation etc. It also makes it easier to spot any errors or copy-and-paste slips.
To get an idea of what it should end up looking like, have a look at some of the formatted code other students have posted in this topic.
Once you’ve formatted and posted the code that isn’t working for you, we’ll do our best to find out what the problem is
//fizzbuzz
var f = “fizz”;
var b = “buzz”;
for(var num = 1; num< 101; num++){
if(num %3 ==0 && num %5 !=0){
console.log(f);}
if(num %5 ==0 && num %3 != 0){
console.log(b);}
if(num %5 ==0 && num %3 ==0){
console.log(f+b);}
if(num %3 !=0 && num %5 !=0){
console.log(num);}
}
I found some help on line for Chess Board so I kind of cheated but learned something too.
// Chess Board
var board = “”;
for(var y = 0; y < 8; y++){
for(var x = 0; x < 8; x++){
if ((x+y) % 2 == 0){
board += " ";
}
else {
board += “#”;
}
}
board += “\n”;
}
console.log(board);
Thanks for the tip
Triangle solution
var rows = 7; var text = "#"; for(i=0;i<rows;i++) { //instead of writing to console.log I chose to write to the page document.write(text + "<br>"); text = text + "#"; }
FizzBuzz Solution
var div3="Fizz"; var div5="Buzz"; var div3_5="FizzBuzz" for(i=1;i<=100;i++){ if (i%3===0 && i%5===0){ console.log(div3_5); } else if(i%5===0){ console.log(div5); } else if(i%3===0){ console.log(div3); } else { console.log(i); } }
Chessboard Solution
var GridSize = 8; var Row1= " # # # #"; var Row2= "# # # # "; for(i=0; i<(GridSize/2); i++){ console.log(Row1); console.log(Row2); }
Variable Chessboard Solution
var GridSize = 10; var Rows = "";
for(x=1; x<=GridSize; x++){
for(i=1; i<=GridSize; i++){
if((x+i)%2===0){ Rows += " "; } else { Rows += "#"; }
}
Rows += “\n”;
}
console.log(Rows);
The last one I couldn’t solve before looking at https://eloquentjavascript.net/code/#2.3 as I couldn’t figure out how to have the first row be different from the second row. I think I kind of get it now, but love to see another solution that’s better understandable.
YouTube has a few videos that go through the ‘fizzbuzz’ exercise, I’m sure the other exercises are out there too, but the ‘fizzbuzz’ exercise is the one I was stuck on.
Here’s one https://www.youtube.com/watch?v=ENWZakSoi_k
print_triangle.js:
let op = ‘#’;
for (let ii=0; ii<7; ii++)
{
console.log(op);
op += “#”;
}
fizzBuzz.js:
for (let a=0; a<=100; a++)
{
if (a%3==0 && a%5==0)
{
console.log('FizzBuzz');
continue;
}
else if (a%3==0) {
console.log('Fizz');
continue;
}else if (a%5==0)
{
console.log('Buzz');
continue;
} else
console.log(a);
}
chessboard.js:
let size = 8, pat1 = ’ #’, pat2 = '# ';
for (let a=0; a <size; a++)
{
if (a%2 === 0) {
console.log(pat1.repeat(size/2));
} else console.log(pat2.repeat(size/2));
}
Hi @Nev,
Well done for persevering, as these exercises are a real challenge if you started this course as a complete beginner. Take a look at this post where I’ve summarized my top tips for approaching these exercises if you’re finding them difficult.
Keep on learning!
HI @Nev,
…BOTH rows AND columns are set at 8: effectively the chessboard’s dimensions i.e 8 x 8.
The first for
loop then iterates over the rows; and within each row, the second for
loop iterates over each column. So it’s probably clearer for someone reading your program to name the variable something like size
or dimension
rather than rows
.
Are you going to post your solution to FizzBuzz for us to have a look at as well?
Nice solutions @FrankB
Just one comment about your control flow in FizzBuzz…
It works fine, but instead of having 3 levels of nested if...else
statements, I think it’s clearer to use a chain of if....else if....else if....else
statements, as follows:
for (n = 1; n <= 100; n++) {
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);
}
}
You also won’t end up with quite as many closing curly brackets
To format your code before posting it here in the forum, click on the </> icon in the menu at the top of this forum’s text editor. This 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, which is then also easier for you to organise with the correct spacing and indentation etc. It also makes it easier to spot any errors or copy-and-paste slips. It should end up looking something like my reformulation of your FizzBuzz, above.
Hi @Matgas,
I doubt you’ll get a response from @javedkhalil, as he posted his solutions 2 years ago. It’s great that you’re looking at other students’ solutions and asking questions about them, but I would direct your questions to more recent posts, as these are the students who are currently doing the course, or have recently finished.
Anyway, having said that, I’ll explain the logic used by @javedkhalil in the inner for
loop in his Looping a Triangle:
The condition column < row
will allow 1 less loop (and therefore 1 less # added) than the current row number e.g.
Row 0 (1st): no loops / +0 #s
Row 1 (2nd): 1 loop / +1 #
Row 2 (3rd): 2 loops / +2 #s
… etc. …
Row 6 (7th): 6 loops / +6 #s
As the outer for
loop always adds 1 # to each row before the inner for
loop starts its iterations across the columns, we will end up with the correct number of #s in each row:
Row 0 (1st) 1 + 0 = 1 #
Row 1 (2nd) 1 + 1 = 2 ##
Row 2 (3rd) 1 + 2 = 3 ###
Row 3 (4th) 1 + 3 = 4 ####
Row 4 (5th) 1 + 4 = 5 #####
Row 5 (6th) 1 + 5 = 6 ######
Row 6 (7th) 1 + 6 = 7 #######
As the triangle is displayed on the web page using document.write()
we need to add a line break to each row (HTML <br>
element). Otherwise we would just display one long row of #s.
I hope this makes it clearer. Just let us know if you have any further questions.
Okay!
This is pretty hard initially if you’re new to this [like me ]
But it’s great to read posts/comments here today, that’s given me good moral support!
I took quite some time over this, and had to start looking for help after quite a few false starts . . and eventually pieced together the way I wanted to approach it from other youtubes, the book, going back over the vids, my javascript command printouts, etc etc. Was very pleased to see it print out at last by the end of the day! Learning a huge amount, and retaining a lot of it also. Feels good
May start FizzBuzz today . . . may need a break . . we’ll see!
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>TriangleTest</title>
</head>
<body>
<script type="text/javascript">
var num_rows = 7
for (var row = 0; row < num_rows; row ++)
{
var toPrint = "#";
for (var column = 0; column < row; column ++)
toPrint += "#";
document.write("<h1>" + toPrint + "</h1>");
}
</script>
</body>
thank you for your instructions.
However, I decided to do a udemy course on Javascript because they go deeper into it, with a much slower pace. Once I finished that one, i return to this course to get an update
Wow, so this was super hard for me and quite overwhelming at first, yikes.
Lots of googling and a little panicking , I can read the code and understand it but really need to improve thinking for myself when implementing it but it is still very early on this journey
.
Here we go;
Triangle:
var output = "";
var outputLength = 7;
while(output.length < outputLength){
output = output + "#";
console.log(output);
}
FizzBuzz:
// This was sooooooo hard!
for(var n = 1; n < 101; n++){
var output = "";
if(n%3 == 0){
output += "Fizz";
}
if(n%5 ==0){
output += "Buzz";
}
else if(output.length == 0){
output = n;
}
console.log(output);
}
Chessboard
// Thinking for this exercise and being able visualise (and run the code a hundred times trial and error) the chessboard I could see the outcome I wanted and this was a bit "easier"
var dimension = 8;
for(var row = 0; row < dimension; row ++){
var output = "";
for(var column = 0; column < dimension; column ++){
if(row%2==0){
var evenChar = " ";
var oddChar = "#";
}
else{
var evenChar = "#";
var oddChar = " ";
}
if(column%2==0){
output += evenChar;
}
else{
output += oddChar;
}
}
console.log(output + "\n");
}
Haha, of course it won’t take odd numbers…as soon as I read that I chuckled to myself - I didn’t pick up on that when I did it (probably because you miss obvious things when you are so deep down the rabbit hole - I was chessboard code crazy ) but now that you mention it, I straight away realised that it is because I defined each line to be either black and white and then increment black + white, depending on the count (or vice versa) and this can clearly only be an even number solution haha. Good pickup…since coding is all about logic however, I would ask, who is going to play chess with an uneven number of pieces and an odd number length/width board? Game, Set, Match. But in all seriousness, thanks for your time and feedback, very much appreciated.
Thank you so much Jonathan. I actually didn’t realize that you could use several ‘else’-statements sequentially. I thought that they always had to be nested—so that’s very helpful. Your use of sequential ‘else’-statements seems similar to the case statement in PASCAL and the ‘switch’-statement in Javascript (is it called ‘switch’?—my memory for names is not very good). I was considering using the ‘switch’-statement, but the decided that it wasn’t really convenient.
By the way, speaking of PASCAL, I have to say that of all the different programming languages that I have encountered over the years (PASCAL, C++, Javascript, Basic, Fortran, Solidity, Python, SQL, and CPL (and Assembler and machine language—but those are on a different page)) PASCAL is the nicest by far. The syntax is beautifully clean, it’s very well structured and it does seem to support object-oriented programming just like C++. So why is not used anymore?
One more thing: I never got any answers to my questions concerning the reading assignment on hash functions. The first three or four questions are more mathematical in nature and maybe too difficult (I was told as much by one of your colleagues), but the rest of the questions should be within reach. I am particularly interested in getting an answer to my question concerning the Merkle tree. Is there any way that you or someone else could take a look and try to come up with an answer? I would greatly appreciate your effort.
Frank
Tough exercises for a non-coder!
(holding only a couple of JS theory classes and very little practice yet).
Understood the programming logic of all exercises.
And managed to replicate triangle, but with some help of already written code.
FizzBuzz was much easier and I managed to do that alone.
Chessboard very daunting, especially the row alternation of starting with “#” and " " respectively. I tried to do it myself, but gave up after several hours. Will need to repeat these exercises all a lot as it was really a fast and furious start for me.
practicepracticepracticepracticepractice…
This is my corrected, but still somewhat primitive solution to FizzBuzz:
<script>
for (var i = 1; i <=100; i++) {
var output = "";
if ((i % 3 == 0) && (i % 5 == 0)){
output += "FizzBuzz";
}
else if (i % 3 == 0){
output += "Fizz";
}
else if (i % 5 == 0){
output +="Buzz";
}
else {
output = i;
}
console.log(output);
}
</script>
The following is a far more eloquent solution to FizzBuzz, after refactoring:
<script>
for (var i = 1; i <=100; i++) {
var output = "";
if (i % 3 == 0) {
output += "Fizz";
}
if (i % 5 == 0) {
output += "Buzz";
}
console.log (output || i);
}
</script>
Fuzz Buzz
<!DOCTYPE html>
<html>
<head>
<h1>This is the Exercise1 </h1>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
// FizzBuzz
for (let a = 1; a<=100; a++){
if(a%15 == 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>
<!-- <img src="im2.jpg" alt="wow"/> -->
</body>
</html>
Chapter 2 Exercise.
Triangle Loops
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”);
}
FizzBuzz Loops
2. for (var num=1; num<=100; num++)
if(num % 3 === 0 && num % 5 ===0){ console.log(“fizzbuzz”)
} else if(num % 3 ===0) {console.log(“fizz”);}else if(num % 5 ===0) {console.log(“buzz”);} else {console.log(i)}
3.Chessboard Loops
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);
To be Perfectly honest i struggled a lot writing out code. I understand the terms in which functions does was in text form as in understanding what i am reading and watching. Put it in to context was much harder then i thought. I used google and tried to get a better grasp and deeper understanding of writing out a programme.