Truman Posted September 4, 2015 Share Posted September 4, 2015 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... Link to comment Share on other sites More sharing options...
moonstarsun Posted September 4, 2015 Share Posted September 4, 2015 (edited) 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 September 4, 2015 by Themoonstarsun 1 Link to comment Share on other sites More sharing options...
davej Posted September 4, 2015 Share Posted September 4, 2015 -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. Link to comment Share on other sites More sharing options...
Ingolme Posted September 4, 2015 Share Posted September 4, 2015 Actually, your third example will not work. Link to comment Share on other sites More sharing options...
davej Posted September 4, 2015 Share Posted September 4, 2015 It seems to work in ff40 -- but it does look like a name collision. I've never needed the two-name form. Link to comment Share on other sites More sharing options...
Truman Posted September 5, 2015 Author Share Posted September 5, 2015 (edited) 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 September 5, 2015 by Truman Link to comment Share on other sites More sharing options...
Hadien Posted September 5, 2015 Share Posted September 5, 2015 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". 1 Link to comment Share on other sites More sharing options...
Truman Posted September 5, 2015 Author Share Posted September 5, 2015 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. Link to comment Share on other sites More sharing options...
Truman Posted September 6, 2015 Author Share Posted September 6, 2015 (edited) 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 September 6, 2015 by Truman Link to comment Share on other sites More sharing options...
davej Posted September 6, 2015 Share Posted September 6, 2015 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. 1 Link to comment Share on other sites More sharing options...
Truman Posted September 7, 2015 Author Share Posted September 7, 2015 (edited) 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 September 7, 2015 by Truman Link to comment Share on other sites More sharing options...
dsonesuk Posted September 7, 2015 Share Posted September 7, 2015 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)); Link to comment Share on other sites More sharing options...
Truman Posted September 8, 2015 Author Share Posted September 8, 2015 (edited) 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 September 8, 2015 by Truman Link to comment Share on other sites More sharing options...
dsonesuk Posted September 8, 2015 Share Posted September 8, 2015 Yes! i did use value within quarter() function in if condition, and when i enter the value of 12 the true statement IS shown. Link to comment Share on other sites More sharing options...
Truman Posted September 9, 2015 Author Share Posted September 9, 2015 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? Link to comment Share on other sites More sharing options...
davej Posted September 9, 2015 Share Posted September 9, 2015 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> Link to comment Share on other sites More sharing options...
dsonesuk Posted September 9, 2015 Share Posted September 9, 2015 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? 1 Link to comment Share on other sites More sharing options...
Truman Posted September 11, 2015 Author Share Posted September 11, 2015 (edited) 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 September 11, 2015 by Truman Link to comment Share on other sites More sharing options...
Truman Posted September 11, 2015 Author Share Posted September 11, 2015 (edited) 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 September 11, 2015 by Truman Link to comment Share on other sites More sharing options...
dsonesuk Posted September 11, 2015 Share Posted September 11, 2015 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. Link to comment Share on other sites More sharing options...
dsonesuk Posted September 11, 2015 Share Posted September 11, 2015 The way you did it divides 12 with 3NO! the function is called instantly, processed (12/4) and returns with value of 3 ending up with 3 mod by 3 = 0 Link to comment Share on other sites More sharing options...
davej Posted September 11, 2015 Share Posted September 11, 2015 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now