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

How can my solution be improved?

JavaScript is the first programming language I'm learning. I wrote this program twice - the first time I felt like my code wasn't very efficient at all, so I took a good long look at it and tried to apply concepts learned elsewhere (via Intro to Programming course, or a trip to the MDN).

This is my final solution, and I'd like to know if it can be improved or otherwise incorporate different, more efficient techniques (note that I used both the && and || operators in two different if statements - I was just trying to test them out).

Also, I was wondering of the best way to handle if the user input is equal to a string, such as "two" - right now I'm restricted to the user input being a number string. Thanks!

var correctAnswers = 0;
var questions = [{question: "What is 1+1?", answer: 2},
                 {question: "What is 2+2?", answer: 4},
                 {question: "What is 4+4?", answer: 8},
                 {question: "What is 8+8?", answer: 16},
                 {question: "What is 16+16?", answer: 32}];

for (var i = 0; i < questions.length; i++) {
  var input = prompt(questions[i].question);
  if (parseInt(input) === questions[i].answer) {
      correctAnswers += 1;
      }
};

alert("All done!");

if (correctAnswers === 5) {
  document.write("<h1>You got " + correctAnswers + " questions right! Here's a gold crown.</h1>")
} else if (correctAnswers === 3 || correctAnswers === 4) {
  document.write("<h1>You got " + correctAnswers + " questions right! Here's a silver crown.</h1>")
} else if (correctAnswers >= 1 && correctAnswers <= 2) {
  document.write("<h1>You got " + correctAnswers + " questions right! Here's a bronze crown.</h1>")
} else if (correctAnswers === 0) {
  document.write("<h1>You got " + correctAnswers + " questions right! Huh. That's no questions right at all! Take up some arithmetic.</h1>")
}

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello!!

It's nice, it worked ok

And...

I was able to score a 5 !!

:D

well done

Brandon Barrette
Brandon Barrette
20,485 Points

To answer this question:

  • Also, I was wondering of the best way to handle if the user input is equal to a string, such as "two" - right now I'm restricted to the user input being a number string. Thanks!

I would add another key to your answer, like word_answer: "two". Then when you check the answer, you can check if the answer was equal to the number or equal to the word. Just make sure to remember to take the user input and process it so the comparison will always work.

Happy Coding =)