Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 06: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
murdr30 is an unknown quantity at this point
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?

<script type="text/javascript">

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.");
}

</script>


</head>
murdr30 is offline   Reply With Quote
Old 12-04-2012, 07:00 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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?
devnull69 is offline   Reply With Quote
Old 12-04-2012, 07:31 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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 07:39 AM..
Philip M is offline   Reply With Quote
Old 12-04-2012, 11:34 AM   PM User | #4
murdr30
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
murdr30 is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
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
murdr30 is offline   Reply With Quote
Old 12-04-2012, 12:12 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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?
devnull69 is offline   Reply With Quote
Old 12-04-2012, 05: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
murdr30 is an unknown quantity at this point
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.
murdr30 is offline   Reply With Quote
Old 12-04-2012, 08:24 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 12-04-2012, 08:29 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by devnull69 View Post
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 08:33 PM..
Old Pedant is online now   Reply With Quote
Old 12-04-2012, 08:38 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


All times are GMT +1. The time now is 08:14 PM.


Advertisement
Log in to turn off these ads.