6/13 orangeCost not defined

Code:

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

It keeps telling me orangeCost is not defined, does anyone know why?

1 Like

Hi, at this line you should use a variable or a parameter (orangeCost)

at this line you should use name of the function (price).

var price = function(orangeCost) {

Here you confused the name of the function with the name of your parameter. The exercise tells you that the function name is orangeCost so it should rather look like this:

var orangeCost = function(price) {

the rest seems to be ok.

2 Likes

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

that’s what I did
try it
https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/1?curriculum_id=506324b3a7dffd00020bf661

1 Like

You actually don’t need the variable declaration part; it can just be:

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

orangeCost(5)

It works just fine.

3 Likes

That one worked! But what I don’t get is that earlier it stated a semi colon should be at the end of every line in a function? There is none in yours though after // console.log(price * 5).

Any reason why its not necessary here?

A semicolon ends a statement but in JS also a linebreak ends a statement and the semicolon is placed automatically by JS. The debates about the semicolons being necessary have gone to almost religious proportions… If you want to minify your code e.g.

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

you definetely need them otherwise they are rather optional. In other programming languages like Java or C++ they are mandatory.

1 Like