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 trialtjeiosajfdksl
Courses Plus Student 8,981 PointsDifficulty understanding variable named 'response'
Here is the code
for (var i = 0; i < questions.length; i+= 1){
question = questions[i][0];
answer = questions[i][1];
response = parseInt(prompt(questions[i][0]));
Could I please know why 'response' seems to be set to either, 50,7 or 21 ; i.e the second element of the three arrays is being assigned to variable 'response' is what it looks like to me.
However although it seems this way, I also see that this can not be the case , since there is the prompt()
function taking user input and assigning it to the variable response here :response = parseInt(prompt(questions[i][0]));
. Then again neither are the original values assigned to array questions being overwritten : 50,7 and 21.
I can't make sense of the logic here could it please be explained? I hope I explained my thinking well.
6 Answers
Ari Misha
19,323 PointsHiya James! It seems like questions variable is an multi-dimensional array. Now i'll skip directly to response variable part. You're simply asking all the questions from the questions array with prompt() method, And waiting for user to reply. As soon as user input their reply , you're capturing it and extracting integer from the input and storing it in response variable. Does that make sense?
~ Ari
tjeiosajfdksl
Courses Plus Student 8,981 PointsHi again Ari , Could you please clarify:
question = questions[i][0], what does the zero refer to here, I guess it can't be 50? The 2nd element here of questions array**** var questions=['how many states are there in the US?>',50]***
answer = questions [i][1]; the 1 refers to 50? (2nd element) here**** ['how many states are there in the US?>',50]***
tjeiosajfdksl
Courses Plus Student 8,981 PointsThank you for your reply Ari.
It makes sense but not completely given the context.
response = parseInt(prompt(questions[i][0]));
This part makes sense:
**** You're simply asking all the questions from the questions array with prompt() method, And waiting for user to reply. ****
But I get confused on here : **** As soon as user input their reply , you're capturing it and extracting integer from the input and storing it in response variable. ****
Just two questions : (1) How can the questions array be used for the task of capturing user input as reply, given that earlier in the program the [0],[1] and [2]: in questions[i][0])) below have all been set to numbers already : 50,7 and 21? Surely a separately created 2D array is needed for that task?
response = parseInt(prompt(questions[i][0]));
Ari Misha
19,323 PointsPrompt method displays a dialog box that prompts the visitor for input. And the syntax for prompt() method is
prompt(text, defaultText)
Prompt method takes a text to display on the screen. And in our case we're using questions array for the text. But there is more to prompt method than just displaying text, it also captures the input from the user. Now as soon as your question pops on the screen , there will be a text area just below the question , waiting for your input. and when you input the answer , the answer gets stored in variable response.
tjeiosajfdksl
Courses Plus Student 8,981 Pointshi there Ari yes I understand prompt function thanks. Why is the questions array being used I do not understand as it is already populated, it it somehow being repopulated, then the elements at the indexes must be overwritten? But this can't be true as response is compared to answer next.
Ari Misha
19,323 PointsNahh in the whole process, the original questions array is intact. The prompt method makes a copy of the array and does the stuff to it and returns it , with original being intact. It all happens behind the scenes. Thats the power of complex algorithms implemented in the javascript by coders like you and me and everyone else. (:
tjeiosajfdksl
Courses Plus Student 8,981 PointsThanks Ari. I see now how it works.
tjeiosajfdksl
Courses Plus Student 8,981 PointsHey Ari,
To better understand can I ask you exactly what is taking place in the computer memory please?
You wrote: 'The prompt method makes a copy of the array and does the stuff to it and returns it , with original being intact'.
So I imagine that a copy of questions array is taken :var questions =['how many states are there in the US?',50],
[how many coninents are there?',7],['how many legs does an insect have?',6]
and it is taken at this point question = questions[i][0]; answer = questions[i][1];
so in first iteration of for loop `question = questions[i][0]
which corresponds to index elements [0,0] namely : ['how many states are there in the US?',50]....But then I am confused because user only enters ONE value but what is getting filled and replaced in the array are two elements :['how many states are there in the US?',50]...
Could you please explain?
tjeiosajfdksl
Courses Plus Student 8,981 PointsHi I will present in more detail what I am asking for (var i = 0; i < questions.length; i += 1) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
response = parseInt(response);
The loop runs 3 times . First a question is asked here response = prompt(question);
and the question in the prompt corresponds to question = questions[i][0];
which corresponds to the first question in the array, 'how many states in the US?, but why does '50' not appear as this is the inner element ie zero index atquestion = questions[i][0];
: user answer is stored in 'response' variable at index i (of a copy of questions array) as iteration number, so then the information must be overwritten? correct?
I would understand if user response was stored in a 1D array like this :
var userResponse = new Array(); for (i=0; i < userResponse.length; i++){
response=prompt(question)
response=parseInt(response);
Then I could see how a single value reponse is compared to answer later on if(response===answer){
tjeiosajfdksl
Courses Plus Student 8,981 Pointstjeiosajfdksl
Courses Plus Student 8,981 PointsI see that the for loop is being used to get prompt() to get user to enter a reply 3 times but where is the reply being stored? I do not understand how it can be in array questions :
response = parseInt(prompt(questions[i][0]));