Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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 )
{
}

else (w > 1000)
{
}

</script>

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 ) {
} else (w > 1000) {
}```
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?

• 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.

• Originally Posted by devnull69
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 ) {
} else (w > 1000) {
}```
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

• 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?

• 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.

• 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.

• 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.

• 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);`

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•