Jump to content
Truman

I need help with this problem

Recommended Posts

I'm practising js on an another site and I do not know how to solve this problem. It's simple and I wrote a code, but it doesn't work.-You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!You want to declare a function that calculates the cost of buying 5 oranges.You then want to calculate the cost of the 5 all together.Write a function that does this called orangeCost().It should take a parameter that is the cost of an orange, and multiply it by 5.It should log the result of the multiplication to the console.Call the function where oranges each cost 5 dollars.-and this is a code that I wrote:var orangeCost = function(price) {var val = price * 5;console.log(val);};orangeCost = (5);I could use some help...

Share this post


Link to post
Share on other sites

Your function is correct however instead of passing arguments into your method call you are initializing a void function. function orangeCost(price) {var val = price * 5;console.log(val);};orangeCost(5);

Edited by Themoonstarsun
  • Like 1

Share this post


Link to post
Share on other sites

 

-You are a creature of habit. Every week you buy 5 oranges. But orange prices keep changing!You want to declare a function that calculates the cost of buying 5 oranges.You then want to calculate the cost of the 5 all together.

 

...unless this is an effect of language translation the wording of the problem sounds ridiculous. I'd go find a different tutorial. We have one...

 

http://www.w3schools.com/js/js_functions.asp

 

You can declare a function in several ways including...

function cost(amt){...}var cost = function(amt){...}var cost = function cost(amt){...}

...but they are not going to behave precisely the same in all cases. It seems unlikely that you would write a function to log a result rather than display or return a value.

Share this post


Link to post
Share on other sites

Actually, your third example will not work.

Share this post


Link to post
Share on other sites

It seems to work in ff40 -- but it does look like a name collision. I've never needed the two-name form.

Share this post


Link to post
Share on other sites

Your function is correct however instead of passing arguments into your method call you are initializing a void function.function orangeCost(price) {var val = price * 5;console.log(val);};orangeCost(5);

Thank you, this works...though I still don't understand why my solution was not accepted. This is a codeacademy problem. Edited by Truman

Share this post


Link to post
Share on other sites

this line is the problem:

orangeCost = (5);

in your example orangeCost was assigned to an anonymous function (Its attached to the orangeCost variable, but the function itself is unnamed).

 

That offending line is actually overwriting the function you wrote, thus deleting it. So the orangeCost variable is no longer pointing to a function, but to a plain 'ol number 5.

 

To call it correctly this should be the valid syntax:

orangeCost(5);

Javascript will see that and think "oh, so you want me to run a function that's stored in the 'orangeCost' variable and give that function the number 5".

  • Like 1

Share this post


Link to post
Share on other sites

Thank you! Now I get it, I thought that the problem might be in the first or second line of my code but it's actually only the last one.

Share this post


Link to post
Share on other sites

Here I come again with another one:Define a function called quarter which has a parameter called number.This function returns a value equal to one quarter of the parameter. (i.e. number / 4;)Call the function inside the if statement's condition (and put in a parameter value!) such that "The statement is true" is printed to the console.and my code is:

var quarter = function(number) {return number/4;}if (quarter() % 3 === 0 ) {console.log("The statement is true");} else {console.log("The statement is false");quarter(8)}

when I call my function with parametar 8 it ejects 2. I don't know how to activate this part:

 if (quarter() % 3 === 0 )
Edited by Truman

Share this post


Link to post
Share on other sites

If you have a function that requires a parameter then it makes no sense to call it without a parameter because it will return undefined or NaN.

  • Like 1

Share this post


Link to post
Share on other sites

OK, so I should put for example number(4), but where should I place it?

 

By the way, quarter(8) brings "The statement is false 2"

Edited by Truman

Share this post


Link to post
Share on other sites

 

By the way, quarter(8) brings "The statement is false 2"

Are you saying this is wrong or right on what you expect?

 

8/4 = 2 correct , now 2 % 3 is not equal to 0 so it is false.

 

If you mean include in console log

 

console.log("The statement is false "+quarter(8));

Share this post


Link to post
Share on other sites

Are you saying this is wrong or right on what you expect?

 

8/4 = 2 correct , now 2 % 3 is not equal to 0 so it is false.

 

If you mean include in console log

 

console.log("The statement is false "+quarter(8));

 

When I put quarter(12) it gives me:

The statement is false3

I don't know why is that so because 12/4 = 3 and 3%3 is 0. The statement should be true...

 

Also, I receive a note: ''Oops, try again. Did you remember to call quarter() inside the if statement with a value that would cause it to print 'The statement is true'?''

Edited by Truman

Share this post


Link to post
Share on other sites

Since you are experimenting with the console and can examine any intermediate values you wish I don't see why you are having a problem figuring this out.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style></style><script>'use strict';var quarter = function(number) {return number/4;};var test = function(k){ var j = quarter(k); if (j % 3 === 0 ) {  console.log(k+'/4=' + j +' which is evenly divisible by 3 since ' + j +'/3 = ' +(j/3)+ ' exactly.'); } else {  console.log(k+'/4=' + j +' which is not evenly divisible by 3 since ' + j +'/3 = ' +(j/3) ); }};for (var i=0 ; i<25 ; i++){test(i);}</script></head><body></body>    </html>

Share this post


Link to post
Share on other sites
Well, now I'm completely lost. When I use value 12 it gives me false. Would you be so kind to just show me the whole code?

 

All i did was add value to if condition calling function, that's it, and it worked fine!

if (quarter(12) % 3 === 0 )

but since you are using function again somewhere else i added value to another variable so i only need to adjust once, again it worked fine as it should.

var value_to_mod = 12; if (quarter(value_to_mod) % 3 == 0) {console.log("The statement is true");} else {console.log("The statement is false");quarter(value_to_mod)}

I suppose you are looking at the current console.log and not older previous logs?

  • Like 1

Share this post


Link to post
Share on other sites

davej, keep in mind that I am supposed to solve codeacademy.com problem ( try yourself: https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/1/3?curriculum_id=506324b3a7dffd00020bf661) and that I'm taking their course. So when I paste your complex code it says that's it is incorrect.

Edited by Truman

Share this post


Link to post
Share on other sites

 

All i did was add value to if condition calling function, that's it, and it worked fine!

if (quarter(12) % 3 === 0 )

but since you are using function again somewhere else i added value to another variable so i only need to adjust once, again it worked fine as it should.

var value_to_mod = 12; if (quarter(value_to_mod) % 3 == 0) {console.log("The statement is true");} else {console.log("The statement is false");quarter(value_to_mod)}

I suppose you are looking at the current console.log and not older previous logs?

This code is correct, says codeacademy, but the thing is that in previous problems we first defined functions which is not a case here. That's why this surprises me because I used different syntax. I didn't even know that this is possibly. I have a lot more to learn...or I simply don't get the methodology that this site uses...

by the way, you didn't define quarter here. It's supposed to be n / 4. The way you did it divides 12 with 3 and the remaining is 0. Setting of this problem demands that we define quarter as n that is divided by 4. Or quarter is already a javascript method so I don't have to write formula n / 4?

Edited by Truman

Share this post


Link to post
Share on other sites

functions on there own usually have a global scope and be accessed any number of times, only those placed inside another function for example, have there scope restricted to inside that parent function and cannot be accessed directly from outside unless going through parent first.

Share this post


Link to post
Share on other sites

I'm not too impressed with CodeAcademy but this seems to pass their test...

// Define quarter here.function quarter(number){return number/4;}if (quarter(24) % 3 === 0 ) {  console.log("The statement is true");} else {  console.log("The statement is false");}// obviously 24/4 = 6 and 6%3 = 0

Apparently they are trying to teach you something about functions and also something about the use of the modulo operator.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...