Don't repeat yourself D.R.Y Help


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/1/1?curriculum_id=506324b3a7dffd00020bf661


I don't understand what I'm supposed to be substituting in this function. Nothing gets me the right answer.


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


#2

Hi tanayag

  1. Instead of returning price it should be orangeCost because that's the variable.
    price is just a place holder so you know what to put in there

2.also in console.log if you want it to display the awnswer just type in orangeCost in console.log not price 5


#3

The assignment asks for a function that takes the parameter, multiplies it by 5 and then logs it to the console. On line 2, you assign your parameter 'price' a value of cost * 5. Cost is undefined. If you call your function, the result will be undefined.


#4

assignment should introduce return syntax...
When I saw it here I tried but it seems it can not execute to console

var orangeCost = function (price)
{
return (price *5);
console.log("Orange" + price);

};

orangeCost("1.9");


#5

In your return statement, you don't need the parentheses. Your console.log won't run after a return statement.

I would remove the quotation marks. Those are meant to denote text strings.


#6

var orangeCost = function (price)
{
return price *5;
console.log("Five Oranges" + price);

};

orangeCost(1.9);

Tnx @biff75
I just wonder why it doesnt print "Five Oranges" in console, just price.


#7

var orangeCost = function(price)
{
console.log("price of oranges" + " " +5*price);
}
orangeCost("5");

By executing above code, i got output like "price of oranges 25" .But after clicking on the save and submit button it gives an error like "Oops, try again. Hmm, it looks like your orangeCost() function doesn't return 25 when the cost of an orange is 5."


#8

It looks like console cannot be reached after return.
Whatever I try its not printing.


#9

Try placing your console.log statement on the line above your return statement in the orangeCost function.


#10

Try using a return statement instead of console.log statement in your function.


#11

Thanks!

This explains it all :slight_smile:

The return keyword simply gives the programmer back the value that comes out of the function. So the function runs, and when the return keyword is used, the function will immediately stop running and return the value.

#12

it works with
var orangeCost = function(price)
{
var cost = price*5;
console.log(cost);
};
orangeCost(5)

Thank you!


#13

cost looks undefined here----


#14

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