Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-02-2014, 10:11 PM   #1
Solid State Member
 
Join Date: Sep 2014
Location: US
Posts: 13
Default Cannot get javascript code to compile

Here are the instructions....

When we call a function, its return value is just the result from running the function. That value can then be used just like any other value in JavaScript!
Look at the if statement starting on line 7. The if statement is checking whether the result of calling the function named quarter is divisible by 3.

Instructions
  1. Define a function called quarter which has a parameter called number.
  2. This function returns a value equal to one quarter of the parameter. (i.e. number / 4
  3. 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.
Here is the code that is given:
// Define quarter here.

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

Here is the code I applied and it comes up error message when I compile it. What am I missing?
// Define quarter here.

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

jcrew is offline   Reply With Quote
Old 10-03-2014, 01:10 AM   #2
Fully Optimized
 
strollin's Avatar
 
Join Date: Mar 2011
Location: USA
Posts: 1,938
Default Re: Cannot get javascript code to compile

You have the function quarter mixed in with the code that calls it. You need to separate the function and put it where it says "// Define quarter here." If you switch the "if ..." line with the "return ..." line in your code you would be pretty close.
__________________

strollin is offline   Reply With Quote
Old 10-03-2014, 07:22 AM   #3
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Cannot get javascript code to compile

Also, you'll never actually hit the log function. Once you reach a "return" line, the function's done and you return to whatever called it.
__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 10-03-2014, 03:24 PM   #4
Fully Optimized
 
strollin's Avatar
 
Join Date: Mar 2011
Location: USA
Posts: 1,938
Default Re: Cannot get javascript code to compile

Quote:
Originally Posted by celegorm View Post
Also, you'll never actually hit the log function. Once you reach a "return" line, the function's done and you return to whatever called it.
Yup, that's why I suggested swapping the "return" line with the line above it.
strollin is offline   Reply With Quote
Old 10-14-2014, 10:26 AM   #5
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 7,999
Default Re: Cannot get javascript code to compile

I believe that the problem is, (from the code you pasted) that you don't really define a quarter function at all.

// Define quarter here.

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


funnily enough you need to do exactly as your comments suggest and define the function quarter.
Code:
//define quarter here
var quarter = function(passed_variable) {
var returned_variable =  passed_variable/4;
return returned_variable;
};

//now you can use the function

if (quarter(12) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:38 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0