...

View Full Version : parseInt Output Trouble



Ephyks
12-10-2010, 04:19 AM
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?

Old Pedant
12-10-2010, 07:39 AM
How can we tell? Show the rest of the page. Most expecially, show the HTML element with id="cost".

Philip M
12-10-2010, 08:34 AM
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

Old Pedant
12-10-2010, 06:45 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum