Don't Repeat Yourself


#1

For this section, my code is working because the console is showing the correct answer, but it is not letting me submit code as it has the error message "Oops, try again. It looks like you didn't define orangeCost." What's the trick to let me pass to next session :slight_smile:

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


#2

Your function name needed to be called orangeCost not price.

And price was supposed to be the argument. :slight_smile:


#3

Cheers! Sorted!
Thanks


#4

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

This code is working..


#5

I'm kind of confused why the following code is repeating twice and therefore not letting me pass:

var orangeCost = function(price) {
console.log("Your oranges this week cost" + " " + price*5);
};

orangeCost(5);


#6

Because you declared the function orangeCost, but it's not executing the supposed calculation.
Ex. You typed:

var orangeCost = function (price) { // you ve declared the function name and parameter;
var totalPrice = price * 5; // here you tell that function to do something for you;
console.log("Your oranges this week cost" + " " + totalPrice);
};
orangeCost(5);


#7

That actually doesn't work either. What ajay_singh said did work and I think it's because his code returns just a value of 25. If you ask the console log to print out any words along with it, you get an error saying that your function did not return cost of 25 for the oranges (even though it plainly shows that it does). I'm thinking this might be a bug!


#8

Having the same problem here – and needing to learn javascript soon since I am about to start javascript in year 12!

Code:

var orangeCost = function(price) {
    var newprice = price * 5;
    console.log("Price for 5 oranges: $" + newprice);
};
orangeCost(5);

Display:

Price for 5 oranges: $25
Price for 5 oranges: $25

The aim is to get the total cost of oranges to 25 after multiplying the price of one orange ($5) by five oranges. However, the output creates the same result twice, and I can't see anywhere that could possibly cause the code to loop through just once.


#9

EDIT: I just re-read through the instructions and have found that the instructions were VERY specific – I really hate this, since coders should feel free to modify the code and still progress through.
EDIT 2: Ah, dangit. I was just reading through and noticed Ajay_Singh has already posted the corrected code.

Accepted code:

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

Quote from the instructions: (note the instructions DOES NOT specify to use strings in the console.log)

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

Display:

25


#10

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