Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2010, 04:19 AM   PM User | #1
Ephyks
New to the CF scene

 
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Ephyks is an unknown quantity at this point
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?
Ephyks is offline   Reply With Quote
Old 12-10-2010, 07:39 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-10-2010, 08:34 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is offline   Reply With Quote
Old 12-10-2010, 06:45 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
output

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 PM.


Advertisement
Log in to turn off these ads.