Don't Understand This:


#1

So, I've just recently started learning JavaScript here, on codecademy. I love it! I really understood it, and like it... I still like it, but this piece of code doesn't make sense to me:

function orangeCost(price)
{
return price * 5;
}
console.log(orangeCost(5));

So I'm working on the functions section of JavaScript, and this just makes ZERO sense to me. I understand all the other function thingies, but this makes no sense to me at all. It doesn't ever tell you about the new way to use a function that it just gave to you (that would be "function orangeCost") It never explains the new function, and I get that it doesn't necessarily have to be part of a variable, but it also doesn't explain how "return" works. Please, I need help!


#2

Try to go back on Introduction to Functions in JS lesson 3 - Function Syntax for your better understanding about functions

You need to declare a function using var and then give it a name orangeCost.

var orangeCost

You need to tell the computer that orangeCost is a function by using the keyword function and add a parameter(of your choice) inside the parentheses. Parameter is a placeholder word that we give a specific value when we call the function.

var orangeCost = function(price){

}

Then add a reusable block of code is between the curly brackets
It should log the result of the multiplication to the console.
In this case, use console.log() function to display the data
Inside the parenthesis of your console.log(), addyour parameter and multiply it by 5

var orangeCost = function(cost){
    console.log(cost * 5);
}

To use the function, we call the function by just typing the function's name, and putting a parameter value inside parentheses after it.

Call the function where oranges each cost 5 dollars

var orangeCost = function(cost){
    console.log(cost * 5);
}
orangeCost(5);

#3

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