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 Improving the Random Number Guessing Game

Reza Zandi
seal-mask
.a{fill-rule:evenodd;}techdegree
Reza Zandi
Full Stack JavaScript Techdegree Student 1,599 Points

Here's my code. Any suggestions?

// Keep ing track of questions answered correctly
var numberQuestionsAnwseredCorrectly = 0



var quiz = alert("It is time to begin the quiz! You will be asked 5 random trivia questions. Depending on how many you answer correctly, you will receive a Gold, Silver, or Bronze crown or no crown at all if you get 0 correct. Good luck! May the gods be in your favor!");

// list of trivia questions
var question1 = prompt("How many Oscars has Leonardo Dicapio won?");
var question2 = prompt("Who was the best quarterback in Colts history");
var question3 = prompt("In what year did former president Barrack Obama get elected?");
var question4 = prompt("How many presidents came before Trump?");
var question5 = prompt("Who won the last Superbowl?");



// if statements for prompts/questions , and adds points to the variable tracking it so we know how many they got right
if (question1 === '1' || question1 === 'one'){ numberQuestionsAnwseredCorrectly += 1};
if (question2.toUpperCase() === 'PEYTON MANNING'){numberQuestionsAnwseredCorrectly += 1};
if (question3 === '2008'){ numberQuestionsAnwseredCorrectly += 1};
if (question4 === '44') {numberQuestionsAnwseredCorrectly += 1};
if (question5.toUpperCase() === 'NEW ENGLAND PATRIOTS' || question5.toUpperCase() === 'PATRIOTS' || question5.toUpperCase() === 'NEW ENGLAND'){
  numberQuestionsAnwseredCorrectly += 1};


// if, else if, else clause to determine the rank of the player based on anwsers 

if (numberQuestionsAnwseredCorrectly > 4){document.write("<h1>You answered all questions 5 correct, you earn the Gold Crown!</h1>")}
    else if (numberQuestionsAnwseredCorrectly > 2){document.write("<h1>You answered " + numberQuestionsAnwseredCorrectly + " out of 5 questions correct, you earn the Silver Crown!</h1>")}
    else if (numberQuestionsAnwseredCorrectly > 0){document.write("<h1>You answered " + numberQuestionsAnwseredCorrectly + " out of 5 questions correct, you earn the Bronze Crown!</h1>")}
    else {document.write("<h1> You anwsered 0 out of 5 questions correct, you earn no crown. Go study up and come back!</h1>")};

3 Answers

Steven Parker
Steven Parker
231,271 Points

I'd put the conditional blocks on separate lines from the "if" statements near the bottom to keep the lines from being so long. But otherwise, great job! :+1:


Update: Python has some really neat features you won't find in most other languages. But you can still make this code much more concise by creating a function to ask a question and get an answer, and then calling it several times.

The immediate response is a nice touch. I still recommend a more conventional block format for readability, with statements on separate lines and semicolons after each statement:

if (question1 === '1' || question1 === 'one') {
  alert("Great job, you got it right!");
  document.write("<p>1. correct</p>");  // (will only display when script finishes)
  numberQuestionsAnwseredCorrectly += 1;
} else {
  alert("Sorry, that was incorrect, good luck on the next one!");
  document.write("<p>1.incorrect</p>");
}

And the comma operator can be very confusing, I'd recommend using it only on rare occasions where it makes a large impact on program verbosity.

Reza Zandi
seal-mask
.a{fill-rule:evenodd;}techdegree
Reza Zandi
Full Stack JavaScript Techdegree Student 1,599 Points

Thanks. I actually saw another students version and I liked so I went ahead and tweaked it so it would show an alert box after each answer and tell you if you got it wrong or right, and also tell you which ones you got wrong and right at the very end.

// Keep ing track of questions answered correctly
var numberQuestionsAnwseredCorrectly = 0



var quiz = alert("It is time to begin the quiz! You will be asked 5 random trivia questions. Depending on how many you answer correctly, you will receive a Gold, Silver, or Bronze crown or no crown at all if you get 0 correct. Good luck! May the gods be in your favor!");


// list of trivia questions and if statements for each question,prints which ones correct vs incorrect, plus adds to the variable counter
var question1 = prompt("How many Oscars has Leonardo Dicapio won?");
if (question1 === '1' || question1 === 'one'){alert("great job, you got it right!"),document.write("<p>1. correct</p>"),numberQuestionsAnwseredCorrectly += 1}
  else{alert("Sorry, that was incorrect.Good luck on the next one!"),document.write("<p>1.incorrect</p>")};

var question2 = prompt("Who was the best quarterback in Colts history");
if (question2.toUpperCase() === 'PEYTON MANNING'){ alert("great job, you got it right!"),document.write("<p>2. correct</p>"),numberQuestionsAnwseredCorrectly += 1}
  else{alert("Sorry, that was incorrect.Good luck on the next one!"),document.write("<p>2.incorrect</p>")};

var question3 = prompt("In what year did former president Barrack Obama get elected?");
if (question3 === '2008'){ alert("great job, you got it right!"),document.write("<p>3. correct</p>"),numberQuestionsAnwseredCorrectly += 1}
  else{alert("Sorry, that was incorrect.Good luck on the next one!"),document.write("<p>3.incorrect</p>")};

var question4 = prompt("How many presidents came before Trump?");
if (question4 === '44') { alert("great job, you got it right!"),document.write("<p>4. correct</p>"),numberQuestionsAnwseredCorrectly += 1}
  else{alert("Sorry, that was incorrect.Good luck on the next one!"),document.write("<p>4.incorrect</p>")};

var question5 = prompt("Who won the last Superbowl?");
if (question5.toUpperCase() === 'NEW ENGLAND PATRIOTS' || question5.toUpperCase() === 'PATRIOTS' || question5.toUpperCase() === 'NEW ENGLAND'){
  alert("great job, you got it right!"),document.write("<p>5. correct</p>"),numberQuestionsAnwseredCorrectly += 1}
  else{alert("Sorry, that was incorrect.Good luck on the next one!"),document.write("<p>5.incorrect</p>")};



// if, else if, else clause to determine the rank of the player based on anwsers 

if (numberQuestionsAnwseredCorrectly > 4){document.write("<h1>You answered all questions 5 correct, you earn the Gold Crown!</h1>")}
    else if (numberQuestionsAnwseredCorrectly > 2){document.write("<h1>You answered " + numberQuestionsAnwseredCorrectly + " out of 5 questions correct, you earn the Silver Crown!</h1>")}
    else if (numberQuestionsAnwseredCorrectly > 0){document.write("<h1>You answered " + numberQuestionsAnwseredCorrectly + " out of 5 questions correct, you earn the Bronze Crown!</h1>")}
    else {document.write("<h1> You anwsered 0 out of 5 questions correct, you earn no crown. Go study up and come back!</h1>")};
Reza Zandi
seal-mask
.a{fill-rule:evenodd;}techdegree
Reza Zandi
Full Stack JavaScript Techdegree Student 1,599 Points

here's my snapshot. https://w.trhou.se/xkwpbi36kz

actually added the correct answers in there if the user got it wrong.

I can see what you mean by the conditionals, I'll keep that in mind next time, the lines can get pretty long. I'm used to python where you just mash shit in together though I think python requires less code than JS.