6. Don't Repeat Yourself


#1

var orangeCost = function(price) {
price * 5;
}
console.log(orangeCost(5) + " dollars is total price of your oranges!");

This code works great cause I can procceed to the next question. But my output is:
undefined dollars is total price of your oranges!

Why is this undefined? cause you see that the price gets a 5.


#2

A function returns undefined if a value was not returned.

you have to use return statement in your function like

var orangeCost = function(price) {
return price * 5;  // return somthing
}

for more details what is undefined check it here


#3

Okay that all makes sense now, I can't believe if forgot to return it. I'm used to programming C#, PHP and Ruby. So i don't get why i forgot to return it :stuck_out_tongue:. Thanks man! :smiley:


#4

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