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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Lilian Zhao](https://secure.gravatar.com/avatar/fd2e2eeb23f1d9f201c1b485ccae4d7b?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Lilian Zhao
885 PointsTrying to use For condition and String, but it did not work! Ask help!
My code is as follows. Anyone can correct it?
var quizAnswer = ["Lilian","25","Designer","Blue","Dog"];
quizAnswer[0] = prompt("What is my name?"); quizAnswer[1] = prompt("What is my age?"); quizAnswer[2] = prompt("What is my occupation?"); quizAnswer[3] = prompt("What is my favorite color?"); quizAnswer[4] = prompt("What is my favorite pet?");
var NumOfCorrect=0;
var length = length.quizAnswer;
for(var i=0;i<length;i++){
if(quizAnswer[i]===true){
NumOfCorrect = NumOfCorrect+1;
}
}
document.write(NumOfCorrect);
console.log(NumOfCorrect);
var x;
switch(NumOfCorrect){
case 5: x="Gold Crown";
break;
case 4: x="Silver Crown";
break;
case 3: x="Silver Crown";
break;
case 2: x="Bronze Crown";
break;
case 1: x="Bronze Crown";
break;
case 0: x="No Crown";
break;
}
document.write(x);
1 Answer
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsI see a few issues:
- the assignments of the results of calling prompt overwrite the array that holds the actual answers
- you probably need a different array to hold the user answers (perhaps "userAnswer"?)
- you wrote: "
length.quizAnswer
" but you probably intended "quizAnswer.length
" - instead of comparing "
quizAnswer[i]
" to true, you probably want to compare it with "userAnswer[i]
"
Fixing those issues should get you going.
In future, when posting code, be sure to format it using the instructions from the Markdown Cheatsheet found below the "Add an Answer" area
Lilian Zhao
885 PointsLilian Zhao
885 PointsHi Steven, thanks so much for correcting! It works!