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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

Trying 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);

Hi Steven, thanks so much for correcting! It works!

1 Answer

Steven Parker
Steven Parker
231,269 Points

I 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 :arrow_heading_down: