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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

if/else statement keeps printing the same answers.

I'm working on the quiz and for whatever reason my if/else statement continues to print out 'absolutely' no matter what I type in. I was wondering what I could possibly be doing wrong to continue getting this answer. Can you check my code out and give me some insight?

This is the Javascript code for the first question. It's all I got right now:

function print(message) { document.write(message); }

var wcount = 0;

var rcount = 0;

var qa = [ ["What's the best language?", "Javascript"],

["What language is named after a gem?", "Ruby"],

["Train is to tracks as Ruby is to...", "Rails"] ] var a1 = qa[0][1];

var q1 = prompt(qa[0][0]);

if (a1 === "Javascript") {

alert("Absolutely");

rcount ++;

} else {

alert("Sorry, wrong answer");

wcount ++; }

1 Answer

Dan Weru
Dan Weru
47,649 Points

Hi Anna,

Your if condition keeps evaluating to true at all times because of this line

var a1 = qa[0][1];

That sets the value of variable a1 to β€˜JavaScript’. To avoid that, do not set a1 manually, instead set it’s value to the value submitted after filling the prompt dialogue.

Edit you answer to be as follows

function print(message) { document.write(message); }

var wcount = 0;

var rcount = 0;

var qa = [ ["What's the best language?", "Javascript"],

["What language is named after a gem?", "Ruby"],

["Train is to tracks as Ruby is to...", "Rails"] ];

var a1 = prompt(qa[0][0]);

if (a1 === "Javascript") {

alert("Absolutely");

rcount ++;

} else {

alert("Sorry, wrong answer");

wcount ++; }

Thanks for that. I actually finished it. I'm stoked.

Dan Weru
Dan Weru
47,649 Points

Great. Fun coding!