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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parseInt Output Trouble

    Hey. I need some javascript help. Quite confused. So I made a .js file with the following contents.

    function cost (){
    var q = "How much do you want to spend for a Hotel?"
    var newb1 = prompt(q, "")
    var q = "How much do you want to spend for Food?"
    var newb2 = prompt(q, "")
    var q = "How much do you want to spend for Activities?"
    var newb3 = prompt(q, "")
    var q = "How much for Shopping?"
    var newb4 = prompt(q, "")
    sum2 = parseInt(newb1) + parseInt(newb3) + parseInt(newb4)
    document.getElementById("cost").innerHTML = "Total Price :" +sum2
    }

    Then I inserted it into my homepage using this.
    <script type="text/javascript" src="hotelprice.js"></script>

    The prompt comes up, asks for all of the inputs. But it doesn't display the output, which should have simply added them all together. How do I fix this?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    How can we tell? Show the rest of the page. Most expecially, show the HTML element with id="cost".
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,036
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Well, if you have in your page
    <span id = "cost"></span>
    it works fine except you have left out parseInt(newb2) from your addition.

    But there are no checks that the user enters numbers - what if he enters "Mickey Mouse" or "-250"? A good chunk of coding involves input validation.

    There is no need to re-declare var q repeatedly.

    Although it is not mandatory to place semi-colons ; at the line ends it is strongly recommended that you do so. Otherwise a nasty gremlin called automatic semi-colon insertion will get you one day.

    Durex Extra Safe Condoms 24-Pack.
    Buy Now: £3.32
    4 Used and New from £1.00.
    In stock.
    - Amazon

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Quote Originally Posted by Philip M View Post
    Durex Extra Safe Condoms 24-Pack.
    Buy Now: £3.32
    4 Used and New from £1.00.
    In stock.
    - Amazon
    ROTFLMAO! You come up with some real winners, Philip!
    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.


  •  

    Tags for this Thread

    Posting Permissions

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