/    Sign up×
Community /Pin to ProfileBookmark

problem with +

hi,this is my first time on this forum and i’m new with javascript.I have problem with this:
when i write 2+3 i get 23 instead 5.when i using *,/ or – everything working fine.only problem is with +.

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 10.2004 — The first alert shows numeric addition, the second string addition:
<script type="text/javascript">
<!--
var total=2+3;
alert(total);
total="2"+"3";
alert(total);
//-->
</script>
Copy linkTweet thisAlerts:
@steelersfan88Oct 10.2004 — [i]Originally posted by Fang [/i]

[B]The first alert shows numeric addition, the second string addition[/B][/QUOTE]
(also called addition, and concatenation, respectively, for the record of Struxo)
Copy linkTweet thisAlerts:
@struxoauthorOct 10.2004 — Thanx,i tried but nothing works.here is my code:

res1=(agor+afor)/amatch;

res2=(bgor+bfor)/bmatch;

res=(res1+res2)/2;

document.write(res);
Copy linkTweet thisAlerts:
@sciguyryanOct 10.2004 — Try:

<i>
</i>&lt;script type="text/javasacript"&gt;
&lt;!--
res1 = (eval(agor) + eval(afor)) / amatch);
res2 = (eval(bgor) + eval(bfor)) / bmatch);
res = (res1 + res2) / 2;
//--&gt;
&lt;/script&gt;



Looks like you were using strings not numeric values ?



RyanJ
Copy linkTweet thisAlerts:
@FangOct 10.2004 — If variables are string you will not get the expected result.

Are any variables from an input element? These are always strings.
Copy linkTweet thisAlerts:
@steelersfan88Oct 10.2004 — sciguyryan ... eval isn't needed here, and since it takes a lot of memory t do, you'd rather avoid it. You can convert to numbers by the [font=monospace]Number([i]string[/i])[/font] function.

You best option is to define the agor, afor, bgor, and bfor as:var afor = new Number([i]f.afor.value[/i]);
var agor = new Number([i]f.agor.value[/i]);
var bfor = new Number([i]f.bfor.value[/i]);
var bgor = new Number([i]f.bgor.value[/i]);
(The italicized probably need changing ...
Copy linkTweet thisAlerts:
@sciguyryanOct 10.2004 — [i]Originally posted by Fang [/i]

[B]If variables are string you will not get the expected result.

Are any variables from an input element? These are always strings. [/B]
[/QUOTE]



Yes, thats the problem. It looks like they are all from either a prompt box or an input element.

RyanJ
Copy linkTweet thisAlerts:
@steelersfan88Oct 10.2004 — Congrats on you passing 1000 btw, guess i'm a tad late, but oh well ?

it doesn't appear struxo is online anymore, so we won't know for a while, unless struxo is invisible ...
Copy linkTweet thisAlerts:
@sciguyryanOct 10.2004 — [i]Originally posted by steelersfan88 [/i]

[B]Congrats on you passing 1000 btw, guess i'm a tad late, but oh well ?



it doesn't appear struxo is online anymore, so we won't know for a while, unless struxo is invisible ... [/B]
[/QUOTE]


Thanks and yup... I always thought that feature was reserved for Admins and Mods but I must be thinking of another forum system ?.


RyanJ
Copy linkTweet thisAlerts:
@steelersfan88Oct 10.2004 — [i]Originally posted by sciguyryan [/i]

[B]Thanks and yup... I always thought that feature was reserved for Admins and Mods but I must be thinking of another forum system[/B][/QUOTE]
I'm surprised how many people don't know about it. I was inspired by an antagonist who never appeared online.

... Numerous people don't know about the online list or forums group list, either

</off-topic-ness>
Copy linkTweet thisAlerts:
@struxoauthorOct 10.2004 — hi,thanx for replays.here is the whole program.i hope you can see mistakes.in txt boxes i put nummbers.

<html>

<body bgcolor = gray>

<script type="text/javascript">

function overunder()

{

var ovun=document.calc.ou.value;

var amatch=document.calc.amatch.value;

var bmatch=document.calc.bmatch.value;

var agor=document.calc.agor.value;

var afor=document.calc.afor.value;

var bgor=document.calc.bgor.value;

var bfor=document.calc.bfor.value;

res1=(agor+afor)/amatch;

res2=(bgor+bfor)/bmatch;

res=(res1 + res2)/2;

if(res<ovun)

{

document.write("<b>UNDER</b>");

}

if(res>ovun)

{

document.write("<b>ÖVER</b>");

}

if(res==ovun)

{

document.write("<b>EXAKT</b>");

}

}

</script>
<form name="calc">
O/U: <input type="text" name="ou" size="8">


<br /> <br/> <br /> <br/>Lag A<br />

Antal matcher: <input type="text" name="amatch" size="8">

<br />

Mål +/-: <input type="text" name="agor" size="8"><input type="text" name="afor" size="8">

<br /> <br/> <br /> <br/>Lag B<br />

Antal matcher: <input type="text" name="bmatch" size="8">

<br />

Mål +/-: <input type="text" name="bgor" size="8"><input type="text" name="bfor" size="8">

<br /> <br/> <br />

<input type=button value="Visa resultat"

onClick="overunder()">

</form>
</body>


</html>
Copy linkTweet thisAlerts:
@sciguyryanOct 10.2004 — Try this:

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html&gt;
&lt;body bgcolor="gray"&gt;
&lt;script type="text/javascript"&gt;
function overunder(){
var ovun = new Number(document.calc.ou.value);
var amatch = new Number(document.calc.amatch.value);
var bmatch = new Number(document.calc.bmatch.value);
var agor = new Number(document.calc.agor.value);
var afor = new Number(document.calc.afor.value);
var bgor = new Number(document.calc.bgor.value);
var bfor = new Number(document.calc.bfor.value);
var res1 = ((agor + afor) / amatch);
var res2 = ((bgor + bfor) / bmatch);
var res = ((res1 + res2) / 2);
if(res &lt; ovun){
document.write("&lt;b&gt;UNDER&lt;/b&gt;");
}
if(res &gt; ovun){
document.write("&lt;b&gt;ÖVER&lt;/b&gt;");
}
if(res == ovun){
document.write("&lt;b&gt;EXAKT&lt;/b&gt;");
}
}
//--&gt;
&lt;/script&gt;
&lt;form id="calc"&gt;
O/U: &lt;input type="text" id="ou" size="8" /&gt;
&lt;br /&gt; &lt;br/&gt;&lt;br /&gt;&lt;br/&gt;Lag A&lt;br /&gt;
Antal matcher: &lt;input type="text" id="amatch" size="8" /&gt;
&lt;br /&gt;
Mål +/-: &lt;input type="text" id="agor" size="8" /&gt;&lt;input type="text" id="afor" size="8" /&gt;
&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;br/&gt;Lag B&lt;br /&gt;
Antal matcher: &lt;input type="text" id="bmatch" size="8" /&gt;
&lt;br /&gt;
Mål +/-: &lt;input type="text" id="bgor" size="8" /&gt;&lt;input type="text" id="bfor" size="8" /&gt;
&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;
&lt;input type="button" value="Visa resultat" onClick="overunder()" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


RyanJ
Copy linkTweet thisAlerts:
@struxoauthorOct 10.2004 — thank you very much.works perfect
Copy linkTweet thisAlerts:
@sciguyryanOct 10.2004 — No problem, Glad to help.


RyanJ
×

Success!

Help @struxo 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.17,
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,
)...