Hey there,
I’m just learning here myself alongside with you. But let me take a shot at answering your question in a different way. Because I think I understand where the confusion is coming in here.
Referencing this line of code:
let priceOf = (item) => {
return item.price;
};
Lets say instead of using return item.price
we used instead replaced it with console.log(item.price)
. So now our function declaration looks like this:
let priceOf = (item) => {
console.log(item.price);
};
Now whenever we call the priceOf
function, all the function will ever do is print to the console the price of the requested item. And in the context of this lesson, that’s exactly what we want it to do!
So then farther down when we call the function, instead of having to write this line asking it to print the result of the function to the console:
console.log(priceOf(groceries[0]));
We could instead just call the function by typing:
priceOf(groceries[0]);
And it would automatically know that we want it to print the price of the item to the console, effectively having the same result. Hurray!
However, let us go ahead and consider an alternate scenario.
What if instead of just printing the price of the item to the console we wanted use the price value for a different purpose? For example, adding two prices together to find their total cost. For this next example lets use this groceries array:
let groceries = [
{name: 'Grasshopper Gumdrops', price: 4},
{name: 'Bug Juice', price: 8}
];
Now if we call this function we just created above:
let priceOf = (item) => {
console.log(item.price);
};
All the function was told to do is to print the price of the item to the console.
So if we tried adding the price of the 2 grocery items in the above array like this:
priceOf(groceries[0]) + priceOf(groceries[1];
All that would happen is it would print to the console:
4
8
Because when called, those functions are told to print the items price.
Now lets go back and edit our function declaration back to the original example so that it looks like this:
let priceOf = (item) => {
return item.price;
};
Now when we call on this function, instead of the function being told to just go ahead and print the item price, we are asking it to return the price of the item instead. So now lets try using this new function in our addition problem.
priceOf(groceries[0]) + priceOf(groceries[1]);
Since we told it to return the price value (instead of print it), what effectively happens is this:
4 + 8;
Now it’s adding together the prices, but we haven’t told the program what to do with it.
What we could do is assign a variable to hold the sum:
let sum = priceOf(groceries[0]) + priceOf(groceries[1]);
Now the variable sum
contains the number 12
(the sum of 4 + 8
)
Now all we have to do if we want print the sum to the console is type:
console.log(sum);
And in the console window it will now print our sum: 12
The alternative to assigning the prices to a variable though is to simply type:
console.log(priceOf(groceries[0]) + priceOf(groceries[1]));
And once again in the console window the sum will appear: 12
Hopefully that helps and I didnt over explain it too much!
Best of Luck!
@Grasshopper_Ben @Grasshopper_Natalie Feel free to correct,edit, or clarify anything if I’m a bit off anywhere!