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

melissa brown
4,670 Pointsusing variables in for loops unexpected identifier
hi guys im trying to build a version of fizzbuzz. what i want to do is grab the value that the user puts in from the prompt button > convert the value into from a string to a number > and then use it in a for loop. if the user choice is more than 15 i want it to count up to 15 while if the number is less than 15 i want it to only count up to the userChoice number.
$(document).ready(function() {
var userChoice = prompt("Please enter a number")
userChoice = parseInt(userChoice, 10)
userChoice = $(prompt).val();
for (var i = 0; userChoice.length; i++) {
if (userChoice > 15 ) {
(i < 16; i++);
} else if (i < 15 ) {
(i < userChoice.length; i++);
}
}
if ( i % 5 === 0 && i % 3 === 0) {
$("ul").append("<li>fizzbuzz</li>");
} else if (i % 5 === 0 ) {
$("ul").append("<li>buzz</li>");
} else if (i % 3 === 0 ) {
$("ul").append("<li>fizz</li>");
} else {
$("ul").append("<li>" + i + "</li>");
}
};
});
i dont understand how to correctly put variables into for loop or how to collect the value of a variable from a prompt?
3 Answers

LaVaughn Haynes
12,397 PointsOnce you get your user input you can convert it to an integer and use it just like any other number, including in your loop.
When using loops, define your variables outside of the loop and then you can use them in the loop. Using append might be a little harder to grasp if you are learning. Another method that you can try is concatenating strings inside of the loop instead of creating elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fizz Buzz</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
//collect string from prompt
var userChoice = prompt("Please enter a number");
//convert string to integer
userChoice = parseInt(userChoice, 10);
//define var elements
var $ul = $("<ul></ul>"),
$fizzbuzz,
$fizz,
$buzz,
$li;
//set default loop count
var countTo = 15;
//if userChoice is in range, use userChoice instead for loop
if(userChoice < 15 && userChoice > 0){
countTo = userChoice;
}
//modified for loop using elements
//could also use concatenated string
for(var i = 1; i <= countTo; i++){
if(i % 5 === 0 && i % 3 === 0){
$fizzbuzz = $("<li>fizzbuzz</li>");
$ul.append($fizzbuzz);
}else if(i % 5 === 0){
$buzz = $("<li>buzz</li>");
$ul.append($buzz);
}else if(i % 3 === 0){
$fizz = $("<li>fizz</li>");
$ul.append($fizz);
}else{
$li = $('<li>' + i + '</li>');
$ul.append($li);
}
}
//add elements to body
$('body').append($ul);
</script>
</body>
</html>
</html>
The concat method might look like this
if(i % 5 === 0 && i % 3 === 0){
html += '<li>fizzbuzz</li>';
}
Julian Aramburu
11,368 PointsHi Melissa! It seems you are mixing things up in your if statement! You are setting an if statement, then when you open the curly braces you are writing something between parentesis and that's not the proper IF syntax check that out first! Then if you get stuck again comeback for more help!
Cheers!

melissa brown
4,670 Pointsthank you