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

Jonathan Fernandez
Jonathan Fernandez
7,542 Points

Variable in a variable does not update automatically and needs to be declared again?

Hi everyone,

So I'm undertaking the quiz challenge for JavaScript and noticed that the scoreMessage variable will not update each time score is updated.

I just want to clarify if I really need to declare scoreMessage variable again each time the score variable is updated. I hope that this is not the case and there is a better way of getting scoreMessage to update automatically.

Shown below is my code:

var score = 0;
var scoreMessage = "Questions Answered Correctly: " + score + "\n\n";

var answer1 = prompt(scoreMessage + "What country is also a continent?");
if (answer1.toUpperCase() === "AUSTRALIA") {
  score += 1;
}

var answer2 = prompt(scoreMessage + "What color is the sky during the day?");
if (answer2.toUpperCase() === "LIGHTBLUE" || answer2.toUpperCase() === "BLUE") {
  score += 1;
}

var answer3 = prompt(scoreMessage + "What is 5+5");
if (answer3 === '10') {
  score += 1;
}

var answer4 = prompt(scoreMessage + "Is earth a distance of 1 AU from the sun? YES or NO Only");
if (answer4.toUpperCase() === "YES") {
  score += 1;
}

var answer5 = prompt(scoreMessage + "How many days are there in a year? Enter a number only.");
if (answer5 === '365') {
  score += 1;
}

Any feedback will be greatly appreciated! : )

2 Answers

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

You could write it as a function which would take the score and return the message.

function scoreMessage(score){ return "Questions Answered Correctly: " + score + "\n\n"; }

then call it,

I would also create a quiz function that would take in question and answer variables

Jonathan Fernandez
Jonathan Fernandez
7,542 Points

Hi Ryan,

Thanks for your reply and advice on functions..

So in the end I guess I really can't depend on scoreMessage to automatically update itself each time score is updated and really need 2 lines of code in each if statement? I was hoping for a way to automatically update the scoreMessage without calling it or listing this var multiple times for it's update.

Will start using functions as I've just learned about it in the videos ahead. : )

Best, Jonathan

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

it will need some love

var questionsAndAnswers = [ { question: "" answer: "" }, {

    },
    {

    },
]

function game(){ var score = 0; var scoreMessage = "Question Answered Correctly: " + score; for(var i = questionsAndAnswers.length; i > 0; i--){ //prompt or log your scoremessage questionAndAnswers[i][0] //prompt or log for question and take answer as a variable if(/input/ === questionsAndAnswers[i][1]){ //you will need to modify this a little //alert or log "Yay" score++; } else { //alert or log "sorry" }

}
//alert or log "hey game is over"
//alert or log scoreMessage
//could play again by creating an if statement that would run function again

}

game();