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
victor sagrista lopez
1,437 PointsNot sure of how to end the Fizzbuzz Challenge
Hello everyone, I am taking the fizzbuzz challege, I have made it to console.log fizz and buzz but I'm not sure of how to combine them. I have tried with && but I'm not sure (I might have not gotten yet to that part on JS)
Anyways, here's the code:
var x = 1
while (x<100)
{
if (x % 3==0) {console.log("fizz")} else if (x % 5==0) {console.log("buzz")} else if (x % 3==0 && x % 5 ==0) {console.log("fizzbuzz")} else {console.log(x)}
x = x + 1
}
THank you!!
7 Answers
Jeff Busch
19,287 PointsIf you don't put the and condition first in this situation the number that satisfies that condition will be passed up. Lets use the number 15 as an example.
if(x % 3 === 0)
OK, number 15 satisfies the above condition and it is passed up now. The number 15 will never "see" the condition
if((x % 3==0) && (x % 5 ==0))
James Barnett
39,199 PointsI have tried with
&&but I'm not sure
I think you're right. I always use parentheses in situations like this to make it clearer.
Also, that's not very readable code, I'd suggest you run it through a code formatter and a code checker.
victor sagrista lopez
1,437 PointsJeff Busch Sorry, the question is:
How to connect two conditions ( x divisible by 3 AND by 5) inside one parenthesis?
James Barnett I did try a cleaner version of it. Thanks for sharing dirtymarkup, great tool!
var x = 1
while (x<100)
{
if (x % 3==0) {console.log("fizz")}
else if (x % 5==0) {console.log("buzz")}
else if (x % 3==0 && x % 5 ==0) {console.log("fizzbuzz")} // This is the part I'm struggling with
else {console.log(x)
x = x + 1
}
Thanks again!
James Barnett
39,199 Pointselse if (x % 3==0 && x % 5 ==0) {console.log("fizzbuzz")} // This is the part I'm struggling with
Try using more paraenthsis, I think your issue is an order of operations bug.
By the way, it's a best practice to use semicolons at the end of JavaScript lines as a beginner for the simple reason that the rules on when semicolons are required can be complex and if you forget one your program can fail in hard to debug ways.
victor sagrista lopez
1,437 PointsHi James, thanks for the tips. Good point, I gotta check those semicolon! I have been trying with paraenthesis, but it doesn't work. I'm not sure of what's wrong...
Jeff Busch
19,287 PointsHi victor,
Try this:
var x = 1; //<------No semicolon
while (x<100) {
if ((x % 3==0) && (x % 5 ==0)) { //<----This has to be first
console.log("fizzbuzz")
} else if(x % 5==0) {
console.log("buzz")
} else if(x % 3==0) {
console.log("fizz")
} else {
console.log(x)
}
x = x + 1; //<------ this was inside the else statement so the loop was endless
} //<------ Forgot closing brace
Jeff
victor sagrista lopez
1,437 PointsHi Jeff, Thanks a lot, I knew I was close!
So, in order to understand why you put the double condition "if ((x % 3==0) && (x % 5 ==0)) " at the beginning, I understand that the reason was cause it would otherwise interpret both as performing similarly as they did before individually, right? It's a really subtle difference, tricky, thank you!
victor sagrista lopez
1,437 PointsI see it now! Thanks very much. I need to play more with JS to grasp this conceps.
Great examples Jeff!
Víctor
Jeff Busch
19,287 PointsJeff Busch
19,287 PointsWhat's the question? The code challenge question. How about a link to the question.