Kinda lost and need some help


#1




https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/2?curriculum_id=506324b3a7dffd00020bf661#


It works fine, and I passed the lesson, I just need some help with how this works.

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(15);
console.log(newNumber);

Ok I passed my lessen, but I needed to use the "Stuck" button. I understand what a function is, and why we use it. But I do not understand how this code above actually completes. How does it know what number is === number when we are calling newNumber and not number. It is very confusing and if someone could explain I would be very grateful. I am not the smartest guy in the world, but I love to code, and I am trying to get better.


#2

Your timesTwo function takes a number and multiplies it by 2. Then you created a variable called newNumber which equals timesTwo(15). Then you logged newNumber to the console.

In your timesTwo function, number is the parameter timesTwo multiplies by 2. NewNumber is a variable equivalent to timesTwo(15) which executes as 15 * 2. If you called timesTwo again with 10 as the input, the value changes to 20. If you call newNumber again, newNumber is still equal to timesTwo(15).


#3

Yes I understand all that. What I am not understanding is how the computer knows what 'number' is when we NEVER call it or set a var for it. We have newNumber, but thats different from number. So how does the computer know how to complete this?


#4

I don't know how the computer knows either. My assumption is in javascript, number means not a string.


#5

newNumber has no connection in general with number. But when we equate var newNumber = timesTwo(15)
it calls the function timesTwo that takes the number as 15 and does the following code and returns 30 i.e) your newNumber.


#6

Because it is not in double qoutes so it takes as an integer.


#7

Yes we understand this, but basic principle tells us everything has to be called. Studying the code above, there is nothing linking any vars to number. So it is very confusing on how the computer knows what to do.

I understand how the code works, what I do not understand is how the computer knows we are subbing our actual number for their 'number'.


#8

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

I don't understand this 100% but the gist of it seems to be, certain words have universal definitions in javascript.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.