Don't Repeat Yourself (D.R.Y)


Hi can somebody please help me with this code problem?

// function that calculates the cost's of an oranges
var orangeCost = function(price) {
var price = 5 * 5;
console.log("the Price of 5 Oranges is $" + price);


the console prints out :

the Price of 5 Oranges is $25
the Price of 5 Oranges is $25

but it says:

Oops, try again. Hmm, it looks like your orangeCost() function doesn't return 25 when the cost of an orange is 5.


@parasjaved777, it doesn't look like you're actually utilising the function that you called. You have nothing in orangeCost(); hence the error.


Most importantly, there is a problem with how your function is put together.

The variable inside your function should not share names with your input placeholder. We want these two things to hold different data. We can keep the name price as your input placeholder, but let's change the name of the new variable inside the function.

So, we have:

var orangeCost = function(price) {

and we want to declare a variable inside that will hold our calculation. We're going to print this variable later, after it collects our total cost. We'll call it total, and instead of assigning it to 5 * 5 which is fixed, let's assign it to price * 5. This way, when we call our function, it will operate on any value we want to give it.

var orangeCost = function(price) {
    var total = (price * 5);

Finally, it should print out the total variable.

var orangeCost = function(price) {
    var total = (price * 5);

Now we can call the function like this (the 5 in the parentheses can be any number, but for this exercise just give it an input of 5.):


That solves the function problem. The last issue is that the exercise only wants you to print out '25'. So you'll need to remove the "the Price of 5 Oranges is $" string that you added, and only print total.

Happy coding!


Thanks, This was very helpful.


@hleighton that was super helpful!

Just wondering:

couldn't you also return the price * 5? Rather than naming a new var.

Like this?

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

What's the difference?

Any insight welcome!!


You're a quite correct! While my function works, it is an unnecessary addition.


The difference is that yours is more compact, it does the same thing but in less code which is generally good because it helps to keep things clean, the other way can be used still if it helps you to understand better, but as hleighton said it was unnecessary.

I also happened to use that function you just wrote when I did the course. Its more concise and if you can understand it that way its better than writing unnecessary code. However if you dont understand it stick to the longer way if you get that :slightly_smiling:


its wrong code or bug ?



Yes the output is expected to be a number so the additional string brings problems. Anyway it wouldn't be 25 fruits but 25$ or (whatever currency you use) for buying 5 oranges.


@hleighton @benjnev Thanks! I'm still trying to wrap my brain around these :wink: This was helpful clarification!


How are we supposed to know that we need to put "return cost" if that is in the next section?


Ok, good point. Well returning a number instead of a descriptive string would make a whole lot of sense, because it means that you're most likely using this piece of information in the progress whereas console.logging it basically just presents it to the end user. And in this case a language that explains what you see is more useful than just a value. So the best way would probably be using return in the function and console.log to print this value.

But as far as I can see you can also pass the exercise by just using console.log inside of the function and not bothering about return here at all. You just need to make sure that you compute the right value and only print this and with no attachments to it. As @cm90 already showed.


Thank you Hleighton, this was oh-so helpful!


thank you so much hleighton! you basically just taught me what i needed to know :slight_smile: you shd teach this stuff.