Don't Repeat Yourself - What's wrong here?


#1


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

I want the code to look decent (for the skills we've learned so far). My goals are:
* We're told that the price of the oranges changes regularly. So, I want a variable for the price and I want to be able to change that, rather than changing an actual line of code with the constantly updating price.
* I want the result written in correct English.

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

The output in the log is:
This week's orange cost is $25.
This week's orange cost is $25.

My questions are:
1) console.log is only being called once. Why is that being printed twice?
2) If the log says the cost is $25 when the price of a single orange is 5 why is the exercise crying to be about how I'm not getting a return of 25 when the cost of an orange is 5?
3) If I change line 6 (orangeCost(5); to instead have empty parenthesis the log changes. The first line of the log says that the orange cost is "$NaN." and the second line of the log has the correct $25 referenced by price * quantity in the expression. What exactly does that final line of code do?

I don't really like that the weekly quantity is a constant either. I have a simple version where that's turned into a variable also, but that doesn't seem to impact much of anything.


var price = 5
var orangeCost = function(price) {
console.log("This week's orange cost is" + " " + "$" + price*5 + ".");
};
orangeCost(price);


#2

You only need console.log(price * 5); inside orangeCost function because it only asks you to output price and nothing more.


#3

Dude, thank you!

So, this was just a poorly written exercise and because I phrased my console.log in the same basic format of the previous exercises (they just explained how to include spaces and multiple items within a single console.log entry) I failed to meet the exact criteria they were looking for.

The following produces two lines of console.log text. Telling me that This week's orange cost is $25. (twice)

var price = 5
var quantity = 5
var orangeCost = function(price) {
console.log("This week's orange cost is" + " " + "$" + price * quantity + ".");
};
orangeCost(price);

The following code only spits out a single line and it passes the exercise just fine. Thanks!! I'm not clear on why the above code produces two lines and the below code produces a single line. Any help there?

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

If I take the above code and just try to add a dollar sign (to give the number 25 some sort of context) this failed the exercise and also still produces two lines of code.

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


#4

It's hard to tell why this happens, it has something to do with Codecademy interpreter but your example also produces single line of code. It's probably just that Codecademy interpreter sees that your function call produces wrong output so it calls that function once again to show you what you did. I just suggest you to be more careful when reading instructions and to do strictly what's necessary nothing less / nothing more.


#5

To the OP:

" * We're told that the price of the oranges changes regularly. So, I want a variable for the price and I want to be able to change that, rather than changing an actual line of code with the constantly updating price."

I am very new at this coding thing (second day on this website and I use no other code languages), but I think the point of this exercise is that the "variable for the price"; or the parameter, is defined -when you "call the function"-. So when it is used, you declare the parameter simultaneously (rather than, as you say, edit the line of code) and the function computes the total cost in this case based on buying 5 oranges (which you presumably always do).

This lesson took me some long thoughts to digest, but I solved it with the below code:

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

So, calling orangeCost pulls the function [console.log (price*5)] and by writing the (5) after orangeCost you are defining the parameter (substituting the word price ((which is just placeholder text for the parameter)) with the value in parenthesis). This way, you can write orangeCost(anynumber) and you will get the total cost for 5 oranges.

I guess this is a long winded way of saying, you don't need to create var price = 5....as that variable is contained as the parameter of the function!

If it turns out I have completely misunderstood and yet somehow my code passed the lesson...someone please say something to me! :slight_smile:


#6

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

what's wrong here?:frowning:


#7

Is what we see here a perfect example of the blind leading the blind? The instructions are very clear on what to do and what to expect. When we don't deviate and embellish, well, everything works as expected! Don't play games with these courses. Play after passing the module. Not, in the module and on these forums. It is disruptive.


#11

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