Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    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?

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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 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?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 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.
    Last edited by Philip M; 12-04-2012 at 07:39 AM.

    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.

  • #4
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 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?

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

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    Last edited by Old Pedant; 12-04-2012 at 08:33 PM.
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 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.


  •  

    Posting Permissions

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