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

Mauricio Duran
Mauricio Duran
6,167 Points

The Conditional Challenge Quiz - Feedback?

So this was my solution to the challenge. https://w.trhou.se/4fmajyhjs9

I wanted someone's opinion on it and any tips to optimize mi coding. Thanks

Your code is not functioning correctly. It only alerts "are you ready...." sentence but does not ask the questions.

1 Answer

First up, questionsLeft wasn't declared first as a variable using the var keyword, so it will be a global variable. You should include it at the top:

var questionsLeft;

OR, instead of repeating the line:

questionsLeft=' ['+ questions+' questions left]';

...you could make a function called questionsLeft (instead of the variable above) and have it return that value:

function questionsLeft() {
  return ' [' + questions + ' questions left]';
}

Then you would call the function from within each prompt:

var q1=prompt("What is the lightest element in the Periodic Table?" + questionsLeft() );

Alternatively, you could create a function to use the prompt and display the questions left in one go:

function askQuestion(question) {
    return prompt(question + ' [' + questions + ' questions left]');
}

var q1 = askQuestion("What is the lightest element in the Periodic Table?");

You can also chain toLowerCase() directly to the prompt() function call:

var q1=prompt("What is the lightest element in the Periodic Table?" + questionsLeft ).toLowerCase();

Also, it won't change much, but an alternative syntax to adding or subtracting 1 that you could use is:

questions--;  // instead of questions -= 1;
count++;      // instead of count += 1;