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 trialname564681351614
3,497 PointsStumped on the 2nd Question
Question 1:
var person = "Jim";
function whosGotTheFunc() { var person = "Andrew"; }
person = "Nick";
whosGotTheFunc();
console.log(person);
Correct Answer = Andrew
2nd Question:
var person = "Jim";
function whosGotTheFunc() { person = "Andrew"; }
person = "Nick";
whosGotTheFunc();
console.log(person);
Correct Answer was NOT Andrew.
For the above, I realize the term "var" was not within the function within the 2nd question. However, I do not understand why this should matter. During the video Jim talked about how when there is a var within a function and that var name matches a global var name it doesn't impact the global name as it's only that specific var within that function. He then went on to talk about how when you take the var OUT of the function and just put the name = to something, it becomes global. If that's the case how can Andrew not be the correct answer for the second question since the last thing to run in the code is the function containing the term "Andrew"?
2 Answers
haunguyen
14,985 PointsAnswer for q1 is Nick, q2 is Andrew. Please review the concept of scope.
From the top for q2:
initialize the "person" variable and assigned it "jim"
reassign the variable with "nick"
call the function, and reassign the variable with "andrew"
log the variable, which was last assigned the value of "andrew"
Herb Bresnan
10,658 PointsEric, sorry I could not get back to you in time but it's probably for the better because I am, after reading this, a little confused. I thought the answer for Q1 is Andrew. This is because the "var" keyword was used inside the function as such: var person= Andrew. Since the var keyword was used, that means it was defined in a local function. Local functions cannot be changed by Global functions. (This is the main reason for using the var keyword in a function).
Q2 I agree would be Nick. Jim was named a Global variable and outside the scope of the function and was ignored. And since there is no var keyword in the function for Adrew, he was replaced by the last person called, Nick.
name564681351614
3,497 Pointsname564681351614
3,497 PointsHello,
Thank you for such a quick response. Have you tried entering those answers for the quiz? When I took it I said the answer to Q1 was "Andrew". Also, for Q2, I followed the same logic as you state here and put "Andrew" for the answer. However, it told me that was wrong. Which is what spurred my question.
I appreciate the assistance! Any idea why it would tell me that's wrong?
Thanks, Eric
haunguyen
14,985 Pointshaunguyen
14,985 Pointsyep, I verified the answer before I posted. BUT the quiz could be randomizing the order of the question. So, for the question with the "var" declaration inside the function, the answer is nick. The other answer is andrew.