Don't repeat yourself (D.R.Y)


#1


I am having a lot of trouble on the (D.R.Y) of Introduction to Functions in Javascript. It says something about finding the cost of 5 oranges.


var orangeCost = function(price) {
console.log(5.00 * 5 + price);
};
orangeCost(0);


It says that the orangeCost() function doesn't return to 25 when the cost of the orange is 5.
Again, I am not sure if I'm not quite understanding the instructions, but if someone does understand, please explain it to me with the code as well. Thank you.



#2

This line is wrong ,how?
lets get back to instructions..
what it says?

It should take a parameter that is the cost of an orange, and multiply it by 5.

and

It should log the result of the multiplication to the console.

so what you have to do ?

cost multiplied by 5?


#3

Ok. I did that. To finish the problem, I have to type the function and a number in (). What number should I type to solve the problem?

P.S. thanks for helping me and for answering my question.


#4

What number should I type to solve the problem?

hint**
last line of instruction...

Call the function where oranges each cost 5 dollars.


#5

This is the new code that I came up with for the problem:

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

It says that it doesn't look right, but I don't know what else to put to solve this problem.


#6

I think You are forgetting about function that you learned in previous exercise.
No issue tho we will revise..

Suppose I made a function that prints multiplication of two numbers..

var multi = function (num1 , num2) {
    console.log(num1 * num2);
}

Now we defined a function..
Now how to use our function?
To use our function.. we call it..
How to call?
We put name of function and put Exact same number of values inside it (inside the parentheses) separated by commas, that it needs.
As our example function needs two values!

multi(2,3);

It will print : 6

Now your function is orangeCost..

that takes price of orange and calculates price of 5 oranges.

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

In console.log() you're making mistake.

Suppose I have to calculate price of 7 oranges..
I will write something like this..

console.log(7 * price);

But you have to write for 5 oranges?
Try that.

Now last part will be done by you...
As I explained how to create and use (call) a function..
now if a ornage costs 5 (like $5) then can you use your function to calculate price value of 5 oranges?


#7

Thank you so much for your help. I finally understood using the example that you gave me! Thanks to that, I could pass on to the next exercise! Thank you and sorry for so much trouble.


#8

No trouble at all!:slight_smile:
Keep up the good work,cheers! :thumbsup:


#9

Thank you for this! It also helped me. Instead of calling parameter i called function and seeing what you wrote made me realize my mistake. Thanks!


#10

I was also having trouble, but I figured it out thanks to a reply from another user. Anyway, I'm glad this section helped you pass!


#11

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