Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Libby Schumacher-Knight
6,612 PointsFizzbuzz js - extra credit - wondering if this is good or if more efficient way of doing?
Have just completed this extra credit task in intro to programming and wondering if there is a more efficient way of doing it?
var counter = 1
while (counter < 101) {
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);
}
counter = counter + 1;
}
Thanks
5 Answers
miguelcastro2
Courses Plus Student 6,573 PointsLooks good!
Only suggestion, while not necessary I still recommend using the increment operator. It's just a nice shorthand way of performing this operation:
counter = counter + 1;
// Shorthand
counter++;
Libby Schumacher-Knight
6,612 PointsI would stay away from using the single &, pretty sure it is better to use two &&
Found this on stackoverflow http://stackoverflow.com/questions/7310109/whats-the-difference-between-and-in-javascript
seems one & is called a bitwise AND and has to do with comparing numbers / binary stuff
single = is for assigning to a variable, two == work but probably should use 3 === this explains well http://www.w3schools.com/js/js_comparisons.asp
=== does value and type
the first reply in this is quick interesting! http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons
also pretty sure in the videos leading up to this task they said not to use == or !=
I see that I should have used 3! perhaps hadn't covered that when I did this task, also hadn't covered for loops
good to see your answer, thanks
Vanessa Schwegman
1,718 PointsWe must think alike. After I found the same solution as you using WHILE, I was able to replicate it with a FOR loop:
for(var counter=1; counter < 101; counter=counter+1){
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);
}
}
The only difference is that I'm using one "&" and not two "&" symbols to combine conditions in the first IF statement. If the code works both ways, I'm not sure what the difference is, but maybe the reason will become clear as I progress. In the meantime, I just tried "and" and "&" until I found the option appropriate to javascript's syntax. I also originally tried using just one "=" sign and couldn't figure out why the code wasn't working. Then I discovered that the code worked with "<" and ">" and recalled seeing the dual equal signs before in the forum. So I tested it, and my code finally worked! A single equal sign must have a special meaning that alerts the code that a variable is being assigned or renamed so I knew I needed a literal or escaped equal sign. (Luckily, I was already familiar with this concept more generally.) Once I was able to overcome the obstacles with my WHILE loop, I was able to get the FOR loop.
Vanessa Schwegman
1,718 PointsMy < (less than) and > (greater than) signs show up as an empty quote for some reason.
Vanessa Schwegman
1,718 PointsI appreciate the explanation and links! They were very helpful. The && versus & makes sense to me now, and it's clear that && is what is needed since we aren't dealing with binary comparisons. My understanding is increasing bit by bit (please pardon me for never resisting a pun). I understand the == versus === enough to know that === seems to be a safer and more straightforward comparison for most purposes. I don't recall any reference to the so called "evil twins" in the previous videos ("Intro to Programming" as it falls in the PHP track). So now I'm not sure if I simply missed it or if we arrived at the challenge from different places.
Libby Schumacher-Knight
6,612 PointsHey, I did intro to programming in the front end web dev track, within JS. but as I finished it they had updated the course to have a new one called JS basics. so it may have been in that course that it talked about == and !=.
Libby Schumacher-Knight
6,612 PointsLibby Schumacher-Knight
6,612 PointsCool, thanks!