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) Working With Numbers The Mad Libs Challenge Revisited

why does this work but not that

function print(message){
    document.getElementById("display").innerHTML = message;
}
//this code works
var questions = 3;
var questionsLeft =  "[" + questions + " questions left ]";
var one = prompt("one? " + questionsLeft);
questions -=1;
questionsLeft =  "[" + questions + " questions left ]";
var two = prompt("two? " + questionsLeft);
questions -= 1;
questionsLeft =  "[" + questions + " questions left ]";
var three = prompt("three?" + questionsLeft);
var sentence = "<p>If only I could " 
+ one + ", then certainly I would be " 
+ two + ", but now I seem " 
+ three + ".</p>";


//this code does not
var questions = 3;
var questionsLeft =  "[" + questions + " questions left ]";
var one = prompt("one? " + questionsLeft);
questions -=1;
//since var questions is inside var questionsLeft left, why doesn't questionsLeft update when when question is updated?
//the whole questions left line must be inserted here or the code doesn't work properly
var two = prompt("two? " + questionsLeft);
questions -= 1;
//without adding the whole questionsleft line in here 1 is not subtracted...why?
var three = prompt("three?" + questionsLeft);
var sentence = "<p>If only I could " 
+ one + ", then certainly I would be " 
+ two + ", but now I seem " 
+ three + ".</p>";

print(sentence);

1 Answer

Hi John,

The second example doesn't work because "questionsLeft" turns into a literal string after being evaluated once. That is to say, it's no longer "" + var + "" after being evaluated, it's just "".

You could make a function which took the updated "questions" as an argument and added the strings to it each time, then call that function whenever you updated "questions", but this lesson is too early in the JS track to teach functions.

Hope that helps!

"questionsLeft" turns into a literal string after being evaluated once. Ok, there's a part of me that gets this. Looking at this code I also see how it BEGS for a function. That being said, I don't RECALL hearing that concept before "turns into a literal string after being evaluated once". Is is something like prompts return a string no matter what? Thanks for your insight.

This is exactly like a prompt returning a string no matter what. It either returns the string or throws an exception.

A variable holding another variable or a function will become a variable holding a literal once it's evaluated by the JS interpreter. Not to be confused with a variable which IS a function; also, bear in mind that you can also have literal objects with functions defined inside them that remain functions after evaluation.

// Quick examples!
var example1 = "Literal String"; // This becomes a literal.
var example2 = example3(); // This becomes a literal.
var example3 = function() { return "String"; } /* This does not become a literal,
or become a string. It can be called to create a string, though.*/
var example4 = example1; // This becomes a literal.

Evan, thank you so much. So much to learn, so few sulci.