JavaScript challenge

Conditional Challenge / JavaScript with TeamTreehouse

TeamTreehouse track I am in has a challenge related to the current topic I’m learning : JavaScript Conditionals. Here’s the challenge:

Challenge Instructions

  1. Ask at least five questions
  2. Keep track of the number of questions the user answered correctly
  3. Provide a final message after the quiz letting the user know the number of questions he or she got right.
  4. Rank the player. If the player answered all five correctly, give that player the gold crown: 3-4 is a silver crown; 1-2 correct answers is a bronze crown and 0 correct is no crown at all.

I think I did a pretty good job! I’m a total beginner still, but you have to start somewhere… Here’s the code:

var answer1 = 60; // minutes in an hour
var answer2 = 24; // hours in a day
var answer3 = 7;  // days in a week
var answer4 = 52; // weeks in a year
var answer5 = 10; // years in a decade
var score = 0;    // user score

//Question set 1
var guess1 = parseInt(prompt("How many minutes in an hour?"));
if (guess1 === answer1) {
    score += 1;   // rather than score = score + 1
  alert("That's right! Your current score = " + score);
} else {
  alert("Sorry Charlie! Your current score = " + score);
}
//Question set 2
var guess2 = parseInt(prompt("How many hours in a day?"));
if (guess2 === answer2) {
    score += 1;   // rather than score = score + 1
  alert("Nailed it! Your current score = " + score);
} else {
  alert("DOH! Your current score = " + score);
}
//Question set 3
var guess3 = parseInt(prompt("How many days in an week?"));
if (guess3 === answer3) {
    score += 1;   // rather than score = score + 1
  alert("Good job! Your current score = " + score);
} else {
  alert("Ohhh they let you out in public? Your current score = " + score);
}
//Question set 4
var guess4 = parseInt(prompt("How many weeks in an year?"));
if (guess4 === answer4) {
    score += 1;   // rather than score = score + 1
  alert("Check out Einstein over here! Your current score = " + score);
} else {
  alert("HASHTAG FAIL! Your current score = " + score);
}
//Question set 5
var guess5 = parseInt(prompt("How many years in an decade?"));
if (guess5 === answer5) {
    score += 1;   // rather than score = score + 1
  alert("MENSA MATERIAL! Your current score = " + score);
} else {
  alert("Bless your heart... Your current score = " + score);
}
//Final Score is...
document.write("Your score is a whopping " + score + "! Way to go?! ");

//Medal Ceremony
if (score === 5) {
  document.write ("You get a GOLD MEDAL!");
} else if (score === 3 || score === 4) {
  document.write ("You get a SILVER MEDAL!");
} else if (score === 1 || score === 2) {
  document.write ("You get a Bronze MEDAL!");
} else {
  document.write ("You get a participation sticker!");
}

Any thoughts? Keep in mind that I haven’t learned arrays, functions et. al… I’m really enjoying this code learning thing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s