Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Arrays Multidimensional Arrays Build a Quiz Challenge – One Solution

Ziyu Zhang
Ziyu Zhang
Front End Web Development Techdegree Student 7,739 Points

Why doing this will not give me the right result?

Here is my code, I find no matter what answer I gave, the correctAnswer is always 3. I learn that if I correct ' if (answers=quiz[i][1])' to' if (answers==quiz[i][1])', the program run correctly. But it doesn;t make sense why (' if (answers=quiz[i][1])')doesn't work.

const quiz=[
  ['What year is it?','2020'],
  ['What\'s the relative of hamburger','hotdog'],
  ['who is the president of the US?','Donald Trump']

for (let i=0;i<quiz.length ;i++){
  let answers=prompt (quiz[i][0]);
  if (answers=quiz[i][1]){
    correctAnswer ++;


2 Answers

Hi Ziyu,

First of all you're setting let answer to equal to the question phrase and not the actual answer. The answer is located at index 1 like this:

let answers=prompt (quiz[i][1]);

On the next line you're setting answer to equal something new, instead of checking if the value actually equals the one contained inside the answer constant.

= is an ansignment operator.

For comparison you need to use == and ===

Perhaps check W3CSchools for more information https://www.w3schools.com/js/js_operators.asp

Marcus Klausen , Ziyu Zhang code reference to the

answer = prompt (quiz[i][0]);

is just fine. This is setting your input to equal the answer at the current iteration.

Ziyu Zhang issue is exactly the single = assignment operator.

This was my solution that works.

const questions = [

  ['What is the color of an apple?', 'red'],
  ['How many inches in a foot?(number only)', '12'],
  ['What is the color of a banana?', 'yellow']

let correct = 0;

for(let i = 0; i < questions.length; i++){
  let answer = prompt(questions[i][0]);
    answer = answer.toLowerCase();
  if(questions[i][1] === answer){
    correct ++;
document.querySelector('main').innerHTML = `<h1>You got ${correct} questions correct </h1>`;