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

Jason Hill
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Hill
Full Stack JavaScript Techdegree Student 11,399 Points

My solution

Here is my attempt. Having watched the video I see I could have done the ranking more efficiently via operators, and for some reason my check for crowns == 0, does not work. As well as not using the .toUpperCase, that leaves potential for flawed operation I see. It seems like it should to me though..? Just wanted to post my first draft for criticism while I work out the issue to get input on possible other things. Yes, I'm an electrician by trade :D

// Setting crown variable to zero before storing correct answers
  var crowns = 0


// Questions code
  var first = prompt("According to ohms law volts x amps = watts, True of False?");
  if(first == "true" || first == "True") {
      alert("Correct!, you've earned one crown!");
      crowns = crowns + 1;
  } else
      alert("Incorrect!");

  var second = prompt("Correct color phases for a 277v 3 phase panel would be Brown, Orange, Yellow. True of False?");
  if(second == "true" || second == "True") {
      alert("Correct!, you've earned one crown!");
      crowns = crowns + 1;
  } else
      alert("Incorrect!");

  var third = prompt("Correct color phases for a 120v 3 phase panel would be Black, Red, Green. True of False?");
  if(third == "false" || third == "False") {
      alert("Correct!, you've earned one crown!");
      crowns = crowns + 1;
  } else
      alert("Incorrect!");

  var fourth = prompt("According to NEC, maximum distance between supports for MC Cable is _FT?");
  if(fourth == "6" || fourth == "six") {
      alert("Correct!, you've earned one crown!");
      crowns = crowns + 1;
  } else
      alert("Incorrect!");

  var fifth = prompt("The maximum number of current carrying conductors allowed in a single raceway before de-rating must be performed is?");
  if(fifth == "3" || fifth == "three") {
      alert("Correct!, you've earned one crown!");
      crowns = crowns + 1;
  } else
      alert("Incorrect!");

// Crown storage and alerts 

  if(crowns == 5) {
    alert("You've answered 5 out of 5 and earned the gold crown!");
  }
    else if(crowns == 4) {
      alert("You've answered 4 of 5 correct and earn a silver crown!");
    }
    else if(crowns == 3) {
      alert("You've answered 3 of 5 correct and earn a silver crown!");
    }
    else if(crowns == 1 || 2) {
      alert("You've answered less than three correct and earn a bronze crown.");
    }
    else if(crowns === 0) {
      alert("You've answered zero correctly and earn a participation award.");
    }

3 Answers

Steven Parker
Steven Parker
229,732 Points

You can only combine complete comparison expressions with logical operators.

So instead of "if(crowns == 1 || 2)", you might write "if (crowns == 1 || crowns == 2)".

Also, when you get down to the last possible condition, you don't need to test again, you can just use a plain "else" (with no "if").

Steven Parker
Steven Parker
229,732 Points

What was the issue with ".toUpperCase()"? I didn't quite understand that part.

Steven Parker
Steven Parker
229,732 Points

That's where it can be handy, for example, this will catch any spelling:

if (first.toUpperCase() == "TRUE") {