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

Sandro Meschiari
Sandro Meschiari
18,418 Points

is it a mistake if i only used one type of var?

i thought that anyway is only a temporary container that i need for an answer

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Sandro,

If I've understood your question correctly I think you're asking about different ways you can use variables.

All variables use the var keyword as you suggest.

You always declare variables in the first instance with the var keyword but you don't have to give them values.

So this is is valid

var numOfPlayers

and this is valid

var numOfPlayers = 3;

But you can also change the value of the variable. So you could declare a variable and then assign the value later.

var numOfPlayers

//code

numOfPlayers = 3;

So you could either leave it without a value or use a temporary value and then change it later when you need to.

Hi Sandro, hi Jonathan!

Since Sandro didn't give a response yet, I felt free to interpret the question differently.

Sandro, did you mean if you could use only one var answer instead of, answer1, answer2, answer3, etc?

If this is what you meant, well I agree that in this particular program, it is a temporary container we need for the answers, as you suggested - because we don't intend to show the player their answers afterwards. So I believe we could do that. When we'd call the variable again and prompt the next question in it, the answer the user provides will overwrite the answer from the previous question - please correct me if I'm wrong.

Warning ahead: I haven't tested the following code, and I don't know the downsides of writing it like this.

So given that the answers provided by the user are correct, it should look something like this:

// Create a variable for score, which is 0 at the beginning of the game
var score = 0;

// 1. Question - where we define the variable answer
var answer = prompt('What is the capital of Italy?');
if (answer.toUpperCase() === 'ROME') {
  score += 1;
}
console.log(answer.toUpperCase());          // This will print ROME in the console.

// 2. Question - where we ask to redefine (overwrite) the variable answer
answer = prompt('Warsaw is the capital city of which country?');
if (answer.toUpperCase() === 'POLAND') {
  score += 1;
}
console.log(answer.toUpperCase());          // This will print POLAND in the console. ROME is not written there anymore.