/    Sign up×
Community /Pin to ProfileBookmark

Plus operator help/error

I just began learning and coding JavaScript and wrote the following. This code will allow the user to input a desired number into a textbox and then increment it or decrement it using the forum + or – buttons. They can also click on the other forum buttons to modify the current number.

All the operators are able to modify the number entered in the textbox except the + operator. It is the exact same code as the others except it uses the + operator instead. Any suggestions why this + operator does seem to work?

Below is the code:

<html>
<head>
<script type=”text/javascript”>
sum=0
adden=0
function add(objId)
{
adden+=1;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function sub(objId)
{
adden=adden-1;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function times2(objId)
{
adden=adden*2;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function times50(objId)
{
adden=adden*
50;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function div2(objId)
{
adden=adden/2;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function clr(objId)
{
adden=0;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}
function inputValue(objId)
{
val=(document.getElementById(“input”).value)
adden=val;
with (document) obj=getElementById(objId)
with (obj) innerHTML = unescape(adden);
}

</script>
</head>
<body>
<form>
<input type=”button” value=”+” onclick=”add(‘ans’)” >
<input type=”button” value=”-” onclick=”sub(‘ans’)” >
<input type=”button” value=”x2″ onclick=”times2(‘ans’)” >
<input type=”button” value=”x50″ onclick=”times50(‘ans’)” >
<input type=”button” value=”/2″ onclick=”div2(‘ans’)” >
<input type=”button” value=”clear” onclick=”clr(‘ans’)” >
<input type=”text” id=”input” value=”” />
<input type=”button” onclick=”inputValue(‘ans’)” value=”input” />

</form>
<div id=”ans”>0</div>
</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@stymieAug 30.2007 — Try using the increment operator
<i>
</i>adden++;
Copy linkTweet thisAlerts:
@KravvitzAug 30.2007 — In JavaScript the "+" operator serves double duty. It's both the addition operator and the string concatenation operator.

The value of form controls is always a string. The other operators will automatically convert both operands to numbers, but since the addition operator serves double duty, it doesn't.

You need to convert the string to a number. Here's what I consider to be the best way:
var num = '55.5'; // numerical string
num = parseFloat(num)||0;

You could use parseInt() instead of parseFloat() if you only wanted whole numbers. The "||0" is used so that passing an empty string will return 0 instead of the NaN (not a number) value.

By the way, it's best to avoid using with(). Do it like this instead:
function sub(objId)
{
adden=adden-1;
var obj=document.getElementById(objId)
obj.innerHTML = unescape(adden);
}

in this simple case you could simplify it to:
function sub(objId)
{
adden=adden-1;
document.getElementById(objId).innerHTML = unescape(adden);
}

P.S. Why are you using unescape() anyway?
Copy linkTweet thisAlerts:
@PouncingFoxauthorSep 01.2007 — Thanks for your comments.

I will try to rewrite the code. I am glad it can be in simpler form also. I thought that the + operator was acting like a string, thats good to know.

I was using unscape() because I used the set container text javascript function in dreamweaver to look to see how to change the value in a textfield. So with a bunch of playing around I stripped its code down to what you see now. Which still functions but I need to study javascript more to make cleaner code.

Your suggestions are much appreciated.

Thanks,

PouncingFox
×

Success!

Help @PouncingFox 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 5.23,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...