Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Volume calculations

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-04-2012, 07:25 AM PM User | #1 murdr30 New to the CF scene   Join Date: Dec 2012 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts Volume calculations I am new to javascript and need to script a problem. Heres a problem : A container can hold up to 100 cubic ft (ft3), but not exceeding 1,000 (lbs), of chocolate. Having this script the webpage so that as soon as it is opened the browser wil ask the user to enter the dimensions of the box of chocolates (length, width, and height in feet), one at a time through a series of window prompts. After which ask the user to enter the weight of the box of chocolates using another prompt window. Then compare the info obtained with the specifications of the container. if the box of chocolates will not fit the container, tell the user by stating in an alert window "Your chocolates will not fit in the container." If the box of chocolates exceeds the weight limit, you must tell the user through another alert window, "Your chocolates are to heavy for the container." If the box of chocolates will fit and are within the weight limit, you must tell the user through another alert window, "Thank you for shipping with us" so here is what i got but it does not seem to work whats wrong?
 12-04-2012, 08:00 AM PM User | #2 devnull69 Senior Coder   Join Date: Dec 2010 Posts: 2,353 Thanks: 11 Thanked 557 Times in 550 Posts This is your code in a readable format Code: ```function getVolume(l, w, h) { var v; v= l * w * h; return v; } var length, width, height; length = parseFloat(prompt("Enter length")); width = parseFloat(prompt("Enter width")); height = parseFloat(prompt("Enter height")); getVolume(length, width, height); function getWeight() { var w; w = weight; return w; } var weight; weight = parseFloat(prompt("Enter weight")); if (v <= 1000000 && w <= 1000 ) { alert("Thank you for shipping with us!"); } else if (v >1000000 ) { alert("Your chocolates will not fit the container."); } else (w > 1000) { alert("Your chocolates are to heavy for the container."); }``` It seems like you have problems understanding the concepts of "global and local variables" and "functions with return values". 1 - You are accessing the variable w in the global scope (i.e. outside of any function). But you declared w to be local to the function "getWeight". Outside of this function you neither declare nor define it, so it is undefined. 2 - You define a function getWeight, but you never call it 3 - You define and call the function getVolume, but you don't assign the return value to any variable in your global scope. So the result (return value) is lost Can you work with this or do you need additional assistance?
 12-04-2012, 08:31 AM PM User | #3 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,471 Thanks: 200 Thanked 2,469 Times in 2,447 Posts OP - please read the forum Rules and Posting Guidelines regarding thread titles and use of code tags. I would point out that the question is not very sensible - I have never seen a container that measures 100ft x 1 ft x 1ft (100 cu feet). Surely none of the chocolate box dimensions should exceed the corresponding container dimensions. A tip - it is recommended that (as devnull69 shows you) the opening brace { is placed on the same line as the function, if, else, do, while, or for statement and not on the following line. This is because of something known as automatic semi-colon insertion. If you disregard this advice it is looking out and one day it will rise up and bite you in the undercarriage, and create an error which can be hard to find. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 12-04-2012 at 08:39 AM..
12-04-2012, 12:34 PM   PM User | #4
murdr30
New to the CF scene

Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by devnull69 this is your code in a readable format Code: ```function getvolume(l, w, h) { var v; v= l * w * h; return v; } var length, width, height; length = parsefloat(prompt("enter length")); width = parsefloat(prompt("enter width")); height = parsefloat(prompt("enter height")); getvolume(length, width, height); function getweight() { var w; w = weight; return w; } var weight; weight = parsefloat(prompt("enter weight")); if (v <= 1000000 && w <= 1000 ) { alert("thank you for shipping with us!"); } else if (v >1000000 ) { alert("your chocolates will not fit the container."); } else (w > 1000) { alert("your chocolates are to heavy for the container."); }``` it seems like you have problems understanding the concepts of "global and local variables" and "functions with return values". 1 - you are accessing the variable w in the global scope (i.e. Outside of any function). But you declared w to be local to the function "getweight". Outside of this function you neither declare nor define it, so it is undefined. 2 - you define a function getweight, but you never call it 3 - you define and call the function getvolume, but you don't assign the return value to any variable in your global scope. So the result (return value) is lost can you work with this or do you need additional assistance?
thank you so much for your help and i think i will be needing a little more assistance with this as i see you slow walking me to an epiphany but need a more direct route there lol, and again thank you

 12-04-2012, 01:12 PM PM User | #5 devnull69 Senior Coder   Join Date: Dec 2010 Posts: 2,353 Thanks: 11 Thanked 557 Times in 550 Posts It's difficult to show you the direct route if I don't know anything about your level of knowledge. Do you know: - the difference between a local variable and a global variable? How to define them? Scope? - how to call a function? - what a return value is and how to use it after returning from a function?
 12-04-2012, 06:01 PM PM User | #6 murdr30 New to the CF scene   Join Date: Dec 2012 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts I do not know the difference between local and global variables i do know how to call a function but unclear as to where it is placed I do know what a return value is but how to use it after returning from a function im unsure of I know I may seem a little dense but I am just learning this as of a few weeks ago so any and all help will be greatly appreciated.
 12-04-2012, 09:24 PM PM User | #7 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,941 Thanks: 75 Thanked 4,306 Times in 4,273 Posts And not to belabor the obvious, but... You create the function getWeight() but (a) you never actually use the function and (b) if you did, it would be doing nothing at all useful. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
12-04-2012, 09:29 PM   PM User | #8
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,941
Thanks: 75
Thanked 4,306 Times in 4,273 Posts
Quote:
 Originally Posted by devnull69 1 - You are accessing the variable w in the global scope (i.e. outside of any function). But you declared w to be local to the function "getWeight". Outside of this function you neither declare nor define it, so it is undefined.
He also has w in the function getVolume( ), just to confuse things further.

And since he never even *CALLS* the function getWeight(), even if w was global there is nothing that assigns a value to it.

And the same problem exists with variable v. Although he sets that local variable of that name in getVolume() it will not exist at global scope, either.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 12-04-2012 at 09:33 PM..

 12-04-2012, 09:38 PM PM User | #9 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,941 Thanks: 75 Thanked 4,306 Times in 4,273 Posts If you use the keyword var *INSIDE* of a function, then all variables thus defined are accessible *ONLY* in that function. When you return a value from a function, that return value WILL BE IGNORED unless it is assigned to a variable by the caller or used in some expression by the caller. So, just for example, when you did Code: `getvolume(length, width, height);` that call was useless. Because the function declared the variable v inside itself and so it is invisible outside the function *AND* you then ignored the value returned by the function. A reasonable thing to do would have been something like Code: `var volume = getvolume(length, width, height);` __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:15 PM.