/    Sign up×
Community /Pin to ProfileBookmark

simple math problem, need help

ok, I’ve made a calculator that puts the values entered in the input fields in a formula. But somethings is reaallly wrong, it either displays something like 34543… or “NaN”.. If all the inputs are 99 (which they’re by default) it should show 138.6; but it doesn’t ? I need help, please. ? (I’ve done the same thing in php and it works there; but I want to do it in JS because then I can make an interactive form. (Idk, how to use ajax)).
You can see the code in action [URL=”http://www.11sta.com/calc.htm”]Here[/URL].
The code is here:

[CODE]<html>

<head>
<script type=”text/javascript”>
function calc1()
{
var inp1=document.getElementById(‘inp1’).value;
var inp2=document.getElementById(‘inp2’).value;
var inp3=document.getElementById(‘inp3’).value;
var inp4=document.getElementById(‘inp4’).value;
var inp5=document.getElementById(‘inp5’).value;
var inp6=document.getElementById(‘inp6’).value;

document.getElementById(‘ost’).value=0.25*(1.3*(inp1+inp2)+inp3+inp4+(0.5*inp5)+(0.5*inp6));
}
</script>
</head>

<body>
<form>
<input type=”text” id=”inp1″ onkeyup=”calc1()” size=”3″ value=”99″><br />
<input type=”text” id=”inp2″ onkeyup=”calc1()” size=”3″ value=”99″><br />
<input type=”text” id=”inp3″ onkeyup=”calc1()” size=”3″ value=”99″><br />
<input type=”text” id=”inp4″ onkeyup=”calc1()” size=”3″ value=”99″><br />
<input type=”text” id=”inp5″ onkeyup=”calc1()” size=”3″ value=”99″><br />
<input type=”text” id=”inp6″ onkeyup=”calc1()” size=”3″ value=”99″><br />

</form>
<input type=”text” id=”ost” size=”5″>
</body>
</html>
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 25.2009 — input value is always a string not a number. Use parseInt or parseFloat to change type.
Copy linkTweet thisAlerts:
@norraauthorMar 25.2009 — input value is always a string not a number. Use parseInt or parseFloat to change type.[/QUOTE]

Thank you very much! It works now ? (I'm new to JS so I didn't know that about strings. Because in php it's different:p)
Copy linkTweet thisAlerts:
@FangMar 25.2009 — You'll probably need toFixed for the displayed result.
×

Success!

Help @norra spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...