This is hard


#1

You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!
01. You want to declare a function that calculates the cost of buying 5 oranges.
02. You then want to calculate the cost of the 5 all together.
03. Write a function that does this called orangeCost().
04. It should take a parameter that is the cost of an orange, and multiply it by 5.
05. It should log the result of the multiplication to the console.
06. Call the function where oranges each cost 5 dollars.

Do not get these instructions!

The code I have:

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


#2

Hey @codekses! your syntax is wrong. This is how you do it ima show you!

So to create a function just do it the javascript way! Will give you additonal comments!! :smile:

// making a function named price
var price = function(orange){
// making the variable orangeCost that calculates the cost of orange! 1 orange is 5$!
var orangeCost = orange * 5;
// then you log it!
console.log(orangeCost)
}
// Last but not Least, you call the function!
orangeCost(5);

If you still dont understand or have any problems! Reply saying so!


#3

It is still saying that it is a problem with my syntax though @amanuel2


#4

Sorry!!! When i called the function i accidently called orangeCost(5)!!! That shouldnt be it change the last line of the calling to

price(5);

Again im really sorry for this mistake @codekses!! Sorry


#5

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

price(5);

is that it? It says I need a "{" somewhere

@amanuel2


#6

oh the problem is that on your code you didnt need that semicolon at your function! just a minor problem @codekses!

Fix this:

to this:

var price = function(orange)

#7

Hey @codekses,

Here:

You put a semi colon between the function and the first corresponding bracket

THAT EQUALS (DRAMATIC MUSIC)

A SYNTAX PROBLEM!!! (DUN DUN DUNNNNN)

Okay, so the problem is that the semi colon signals the code that it's the end.

It also can disconnect. Like, a for loop!(You'll get to that sooner)

So, what the semi colon does here:

Is that it makes the function get disconnected with its first bracket. WHICH EQUALS a syntax problem, for then there's no use of the function, the function can't do anything, AND there are leftover pieces of these:

Which cannot function. Makes sense? Comprende? :smiley:

Sorry I had a little fun with the dramatic music there. :grin:


#8

Jeezez Christ, sorry for bothering you so, but now it says I didn't define orangeCost

Code:

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

@amanuel2


#9

Huh? This image says otherwise? :smile:


#10

As @amanuel2 has shown, you have to refresh there. :smile:

The code now works fine.


#11

But that is the "What is your name" excersise, I'm on the "D.R.Y" excersise

@amanuel2
@ragezapper


#12

Oh sorry i didnt know that! Yea it gives you that error. The function is supposed to be named orangeCost. So this will do!

var orangeCost = function(price){
    
    console.log("The cost of orange is" + (price*5) + "$");
}

orangeCost(5);

If you have trouble understanding anything please reply saying so!


#13

@amanuel2 Also accidentally copy+pasted the code onto the level What is your name? in Getting Started with Programming.

@codekses Sorry, didn't see that.

Also, you don't really have to do as @amanuel2 shows, just console.log price*5. :smiley:


#14

Yeah, lol! Just doing extra work ! Yea like @ragezapper said you dont need to include that part!


#15

i have memorised this process but i am dont fully understand why you have to log or print the "orangeCost" with console.log. Can someone please explain this to me?Thanks