Jump to content

So im on a CodeCademy lesson for Java

I have to figure out this situation: You are a creature of habit. "Every week you buy 5 oranges. But orange prices keep changing!"

 

This is what i have so far and it comes out as "NaN"

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

 i have no idea what im doing wrong

Noobie programer. :)

Link to comment
https://linustechtips.com/topic/66300-just-need-some-help/
Share on other sites

Link to post
Share on other sites

// broken codevar orangeCost = function (price) {console.log(5 * orangeCost);};orangeCost(5);
// fixed codevar orangeCost = function (price) {console.log(5 * price);};orangeCost(5);

I can't work out how to explain what you did wrong right now maybe some else can

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
https://linustechtips.com/topic/66300-just-need-some-help/#findComment-906761
Share on other sites

Link to post
Share on other sites

The fixes that have been posted above are correct, but since there's no explanation I'll jump in and try to fill that gap.

 

First up, which language you're learning. It may well have just been a typo, but there is a big difference between Java and JavaScript. Since the code you posted is JavaScript, and that Code Academy doesn't teach Java, I'll assume that's what you meant.

 

So, on to the code.

// First of all, you are assigning a function to the orangeCost variable,// asking for it to be passed a value to be named 'price'. This means// that price becomes a variable.var orangeCost = function (price) {    // On this line you are trying to multiply the function, orangeCost    // by 5. You could try and work this out on paper, too, but you    // wouldn't get very far, and neither would the JavaScript engine,    // so it instead returns NaN or 'Not a Number'.    console.log(5 * orangeCost);     // You can test that out with the next line, and you'll see 'function'    // be printed.    console.log(typeof orangeCost);     // The correct operation would be the following, as it is multiplies    // the price variable by 5.    console.log(5 * price);}; // Here you pass in the price, which gets assigned to the 'price'// variable up in your function.orangeCost(5);

 

Hopefully that all makes a little more sense now.

Link to comment
https://linustechtips.com/topic/66300-just-need-some-help/#findComment-907171
Share on other sites

Link to post
Share on other sites

Thanks for

 

// broken codevar orangeCost = function (price) {console.log(5 * orangeCost);};orangeCost(5);
// fixed codevar orangeCost = function (price) {console.log(5 * price);};orangeCost(5);

I can't work out how to explain what you did wrong right now maybe some else can

 

 

 

The fixes that have been posted above are correct, but since there's no explanation I'll jump in and try to fill that gap.

 

First up, which language you're learning. It may well have just been a typo, but there is a big difference between Java and JavaScript. Since the code you posted is JavaScript, and that Code Academy doesn't teach Java, I'll assume that's what you meant.

 

So, on to the code.

// First of all, you are assigning a function to the orangeCost variable,// asking for it to be passed a value to be named 'price'. This means// that price becomes a variable.var orangeCost = function (price) {    // On this line you are trying to multiply the function, orangeCost    // by 5. You could try and work this out on paper, too, but you    // wouldn't get very far, and neither would the JavaScript engine,    // so it instead returns NaN or 'Not a Number'.    console.log(5 * orangeCost);     // You can test that out with the next line, and you'll see 'function'    // be printed.    console.log(typeof orangeCost);     // The correct operation would be the following, as it is multiplies    // the price variable by 5.    console.log(5 * price);}; // Here you pass in the price, which gets assigned to the 'price'// variable up in your function.orangeCost(5);

Hopefully that all makes a little more sense now.

Thanks for both of your's help! I swore that i tried the (5 * price)

Noobie programer. :)

Link to comment
https://linustechtips.com/topic/66300-just-need-some-help/#findComment-910291
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×