Results 1 to 4 of 4

Thread: negative values not showing

  1. #1
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default negative values not showing

    Have a problem, hope someone can help,

    If have worked on a script, where scoredage is a number like 8.5 (meaning years.months) and ageInMonths2 is a number like 60

    <script type="text/javascript">

    /** Function to calculate Scored Age variances**/

    function variance(scoredage, ageInMonths2){


    var yearfield = scoredage.split(".")[0]
    var monthfield = scoredage.split(".")[1]
    var yearfield = yearfield*12

    var scoredmonths = parseInt(monthfield) + parseInt(yearfield)
    var variance = scoredmonths - ageInMonths2
    document.write(variance)

    }

    </script>

    Seems to work fine as long as the final output 'variance' is positive. If the value ends up negative 'e.g. -21 or 0 then all I get displayed is NaN.
    Can't figure out why?

    The output is displayed in a repeat region of a php page and I found I had to put var on all the lines to get something to display on each row.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    var yearfield = scoredage.split(".")[0]
     var monthfield = scoredage.split(".")[1]
    You're calling the split() function twice when it's only necessary once.
    Code:
    var yearfield = yearfield*12
    You've already defined yearfield.
    Code:
    var scoredmonths = parseInt(monthfield) + parseInt(yearfield)
    yearfield is already a number, but if either can't be parsed as an integer this will result in the result being NaN. You must also specify a base when using parseInt(), lest odd things happen (E.G. parseInt("011") is 9, because a leading 0 is interpreted as a sign of an octal number).
    Code:
    function variance(scoredAge, otherAgeMonths) {
      return Math.abs(parseInt((scoredAge = scoredAge.toString().split("."))[0], 10) * 12 + parseInt(scoredAge[1], 10) - otherAgeMonths);
    }
    Last edited by Twey; 05-02-2007 at 03:16 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help.
    Your suggestion didn't quite work- probably my faulty code elsewhere.
    However I modified mine using your ideas and came up with

    function variance(scoredage, ageInMonths2){

    scoredage = scoredage.split(".")
    yearfield = parseInt(scoredage[0], 10)
    monthfield = parseInt(scoredage[1], 10)

    yearfield = yearfield*12

    scoredmonths = monthfield + yearfield
    var variance = scoredmonths - ageInMonths2

    document.write (variance)

    }

    It now works fine.
    Turns out the biggest problem ( all those NaN's) wasn't the javascript but the fact that the mysql table the data was being pulled from was missing data from several fields.

    Thanks for the help I have actually learned quite a bit anout Javascript from solving this problem.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function variance(scoredAge, otherAgeMonths) {
      return Math.abs(parseInt((scoredAge = scoredAge.toString().split("."))[0], 10) * 12 + parseInt(scoredAge[1], 10) - otherAgeMonths);
    }
    Note that this function returns the value, not writing it. If you wanted to write it, you could do:
    Code:
    document.write(variance(age1, age2));
    However, I don't recommend you use document.write().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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