D.R.Y Code


#1

Please help check this code

var orangeCost = function(orangePrice){ 
  totalCost = orangePrice * 5; 
  console.log("The cost of 5 oranges at "+ orangePrice +" dollars per one is: "+totalCost);
 }
orangeCost(5);

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

Meanwhile the out put on the screen : "The cost of 5 oranges at 5 dollars per one is: 25"


#2

That's too perfect. The problem is that the exercise just expects 25 (a number) instead of a whole sentence explaining it. But the best answer would probably be to tell what the 25 means. So to pass the exercise use just console.log(totalCost) but for later keep your original message in mind.


#3

Wow! Got it. Thanks.


#4

Thanks for that, i had wrote a similar answer- my error was too simply much detail in the console.log section :expressionless: . writing console.log(totalCost) fixed it. :grin:


#5

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

This works.


#6

Ok... Well it should not. To be more clear:
Your not using the parameter price, the function is not called anywhere but inside the function so it won't run and if it does it will not stop opening new instances of its own. In case you don't see this if you nest a function call in the function itself and not "guard" it by a reasonable condition the function call will open another instance of the function than again runs the code until it finds again this function call which opens another instance of the function aso...

And last but not least this:

orangeCost *5;

makes barely sense as it means [Function]*5 which is probably something like NaN. So better have a look at the code above to see how it works or ask in case something is not clear.


#7

This is the answer.

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


#8

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