www.webdeveloper.com
Results 1 to 8 of 8

Thread: NaN

  1. #1
    Join Date
    Jun 2013
    Posts
    23

    NaN

    Hello guys,

    I have wrote this function:

    var person = new Object();

    person.lengte = prompt("Wat is uw lengte")
    person.gewicht = prompt("Wat is uw gewicht")
    person.bmi = person.gewicht / (person.length * person.length);

    alert("Uw BMI is:" + " " + person.bmi);



    When i execute the code, everything works, but when the browser alert the alert, it is showing: "Uw BMI is: "NaN".

    I tried this: alert("Uw BMI is:" + " " + person.length); and then it works and will gives the number what has been getting from the prompt.

    Any idea how to fix this? It should work without having the person.bmi = person.gewicht / (..........) having in the last line of the code, the alert line.


    Hopefully someone could help me with this!

    Thanks
    Reckimo

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,374
    length is a property of an object, it can't be used to store a number in.

    Code:
    var person = new Object();
    
    person.len = prompt("Wat is uw lengte")
    person.gewicht = prompt("Wat is uw gewicht")
    person.bmi = person.gewicht / (person.len * person.len);
    
    alert("Uw BMI is:" + " " + person.bmi);
    Now, your next problem is knowing if a person is inputting feet and inches, inches, meters or centimeters.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Jun 2013
    Posts
    23
    Okay, thanks. But how can i fix this?

    Thank you!

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,374
    Read the code I posted.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Dec 2013
    Posts
    21
    Well, for one thing, it seems you are mixing 'languages'.
    For instance "lengte" is what...Dutch? And yet you seem to want that value to produce BMI but have used "length" instead of "lengte".
    Did you really want: person.bmi = person.gewicht / (person.lengte * person.lengte);

    Second, why do you add a " " (space) separately in your alert contents when you could just include an extra space in the first text string? ie: alert("Uw BMI is: " + person.bmi)

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by \\.\ View Post
    length is a property of an object, it can't be used to store a number in.
    True, but not the case here. Maybe more correctly would be if you said that length is a common property for various known objects and because of ambiguity its use (to name a property) should be best avoided.

    For example:

    Code:
    var person = new Object();
    person.length = 21;
    console.log(person.length)
    Setting the length property in this case is completely legal as far as JavaScript is concerned.

    Quote Originally Posted by reckimo View Post
    Hello guys,

    I have wrote this function:

    var person = new Object();

    person.lengte = prompt("Wat is uw lengte")
    person.gewicht = prompt("Wat is uw gewicht")
    person.bmi = person.gewicht / (person.length * person.length);

    alert("Uw BMI is:" + " " + person.bmi);



    When i execute the code, everything works, but when the browser alert the alert, it is showing: "Uw BMI is: "NaN".

    I tried this: alert("Uw BMI is:" + " " + person.length); and then it works and will gives the number what has been getting from the prompt.

    Any idea how to fix this? It should work without having the person.bmi = person.gewicht / (..........) having in the last line of the code, the alert line.


    Hopefully someone could help me with this!

    Thanks
    Reckimo
    The source of your problem is that person.length property doesn't exist and it's value is evaluated to undefined. Any calculations performed on undefined value will be evaluated to NaN which means Not a Number!!!
    Last edited by tech_soul8; 01-24-2014 at 09:58 AM.

  7. #7
    Although you are assuming the prompt results as numbers but they are actually strings. You have to explicitly convert them to numbers to perform calculations

    Here is the solution of your problem

    var person = new Object();
    person.length = Number(prompt("Wat is uw lengte"));
    person.gewicht = Number(prompt("Wat is uw gewicht"));
    person.bmi = person.gewicht / (person.length * person.length);
    alert("Uw BMI is:" + " " + person.bmi);
    You should write some more lines of code to make sure that user is not typing string.

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by zealouss View Post
    Although you are assuming the prompt results as numbers but they are actually strings. You have to explicitly convert them to numbers to perform calculations
    This is true only if addition is performed and if one of two operands is a String rather than a number. This is because when used the plus operator gives advantage to the String concatenation. If you are doing subtraction, division or multiplication then the JavaScript will do the conversion automatically for you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles