/    Sign up×
Community /Pin to ProfileBookmark

What is wrong?

I have this

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<script language=”JavaScript” type=”text/javascript”>
function add(var1a, var2a) {
window.alert(var1a + var2a);
}
</script>
<script language=”JavaScript” type=”text/javascript”>
function subtract(var1b, var2b) {
window.alert(var1b – var2b);
}
</script>
<script language=”JavaScript” type=”text/javascript”>
function multiply(var1m, var2m) {
window.alert(var1m * var2m);
}
</script>
<script language=”JavaScript” type=”text/javascript”>
function divide(var1d, var2d) {
window.alert(var1d / var2d);
}
</script>
<title>T101</title>
</head>
<body bgcolor=”#000000″ text=”#FFFFFF”>
Addition:
<form name=”addform” id=”addform”>
<input type=”text” name=”a1″ />
+
<input type=”text” name=”a2″ />
<input type=”button” value=”Add!” onclick=”add(document.addform.a1.value, document.addform.a2.value)” />
</form>
<br />
Subtraction:
<form name=”subform” id=”subform”>
<input type=”text” name=”s1″ />

<input type=”text” name=”s2″ />
<input type=”button” value=”Subtract!” onclick=”subtract(document.subform.s1.value, document.subform.s2.value)” />
</form>
<br />
Multiplication:
<form name=”mulform” id=”mulform”>
<input type=”text” name=”m1″ />
*
<input type=”text” name=”m2″ />
<input type=”button” value=”Multiply!” onclick=”multiply(document.mulform.m1.value, document.mulform.m2.value)” />
</form>
<br />
Division:
<form name=”divform” id=”divform”>
<input type=”text” name=”d1″ />
/
<input type=”text” name=”d2″ />
<input type=”button” value=”Divide!” onclick=”divide(document.divform.d1.value, document.divform.d2.value)” />
</form>
</body>
</html>
[/CODE]

It;s a calclator, divison, subtraction,and multiplication, bt the addition acts like 5+5=55?

What is wrong.

Thanks for the help

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NintendoGamerSTAug 26.2006 — Well, what's wrong is that while "+" can be used in the case of

"1 + 1"

in Javascript, it is also used to combine strings. Since text boxes, when sent to JS, are handled as strings, you get a result of 5+5 being 55 because the second string that contains "5" is added right after the first string that contains "5".

Sadly, I don't know how to fix this problem, but you would have to get rid of the quotes that are automatically added when a form field is sent to JS.

NGST
Copy linkTweet thisAlerts:
@muazzezAug 26.2006 — [color=olive]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script language="JavaScript" type="text/javascript">

function add(var1a, var2a) {

window.alert( (var1a*1) + (var2a*1));

}

</script>

<script language="JavaScript" type="text/javascript">

function subtract(var1b, var2b) {

window.alert(var1b - var2b);

}

</script>

<script language="JavaScript" type="text/javascript">

function multiply(var1m, var2m) {

window.alert(var1m * var2m);

}

</script>

<script language="JavaScript" type="text/javascript">

function divide(var1d, var2d) {

window.alert(var1d / var2d);

}

</script>

<title>T101</title>

</head>

<body bgcolor="#000000" text="#FFFFFF">

Addition:

<form name="addform" id="addform">

<input type="text" name="a1" />

+

<input type="text" name="a2" />

<input type="button" value="Add!" onclick="add(document.addform.a1.value, document.addform.a2.value)" />

</form>

<br />

Subtraction:

<form name="subform" id="subform">

<input type="text" name="s1" />

-

<input type="text" name="s2" />

<input type="button" value="Subtract!" onclick="subtract(document.subform.s1.value, document.subform.s2.value)" />

</form>

<br />

Multiplication:

<form name="mulform" id="mulform">

<input type="text" name="m1" />

*


<input type="text" name="m2" />

<input type="button" value="Multiply!" onclick="multiply(document.mulform.m1.value, document.mulform.m2.value)" />

</form>

<br />

Division:

<form name="divform" id="divform">

<input type="text" name="d1" />

/

<input type="text" name="d2" />

<input type="button" value="Divide!" onclick="divide(document.divform.d1.value, document.divform.d2.value)" />

</form>

</body>

</html>[/color]


[color=brown]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script language="JavaScript" type="text/javascript">

function add(var1a, var2a) {

window.alert( (var1a / 1) + (var2a / 1));

}

</script>

<script language="JavaScript" type="text/javascript">

function subtract(var1b, var2b) {

window.alert(var1b - var2b);

}

</script>

<script language="JavaScript" type="text/javascript">

function multiply(var1m, var2m) {

window.alert(var1m * var2m);

}

</script>

<script language="JavaScript" type="text/javascript">

function divide(var1d, var2d) {

window.alert(var1d / var2d);

}

</script>

<title>T101</title>

</head>

<body bgcolor="#000000" text="#FFFFFF">

Addition:

<form name="addform" id="addform">

<input type="text" name="a1" />

+

<input type="text" name="a2" />

<input type="button" value="Add!" onclick="add(document.addform.a1.value,



document.addform.a2.value)" />

</form>

<br />

Subtraction:

<form name="subform" id="subform">

<input type="text" name="s1" />

-

<input type="text" name="s2" />

<input type="button" value="Subtract!" onclick="subtract(document.subform.s1.value,



document.subform.s2.value)" />

</form>

<br />

Multiplication:

<form name="mulform" id="mulform">

<input type="text" name="m1" />

*

<input type="text" name="m2" />

<input type="button" value="Multiply!" onclick="multiply(document.mulform.m1.value,



document.mulform.m2.value)" />

</form>

<br />

Division:

<form name="divform" id="divform">

<input type="text" name="d1" />

/

<input type="text" name="d2" />

<input type="button" value="Divide!" onclick="divide(document.divform.d1.value,



document.divform.d2.value)" />

</form>

</body>

</html>[/color]


[color=red]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script language="JavaScript" type="text/javascript">

function add(var1a, var2a) {

window.alert( (var1a*1) + (var2a*1));

}

</script>

<script language="JavaScript" type="text/javascript">

function subtract(var1b, var2b) {

window.alert(var1b - var2b);

}

</script>

<script language="JavaScript" type="text/javascript">

function multiply(var1m, var2m) {

window.alert(var1m * var2m);

}

</script>

<script language="JavaScript" type="text/javascript">

function divide(var1d, var2d) {

window.alert(var1d / var2d);

}

</script>

<title>T101</title>

</head>

<body bgcolor="#000000" text="#FFFFFF">

Addition:

<form name="addform" id="addform">

<input type="text" name="a1" />

+

<input type="text" name="a2" />

<input type="button" value="Add!" onclick="add(parseFloat(document.addform.a1.value), parseFloat(document.addform.a2.value))" />

</form>

<br />

Subtraction:

<form name="subform" id="subform">

<input type="text" name="s1" />

-

<input type="text" name="s2" />

<input type="button" value="Subtract!" onclick="subtract(parseFloat(document.subform.s1.value), parseFloat(document.subform.s2.value))" />

</form>

<br />

Multiplication:

<form name="mulform" id="mulform">

<input type="text" name="m1" />

*


<input type="text" name="m2" />

<input type="button" value="Multiply!" onclick="multiply(parseFloat(document.mulform.m1.value), parseFloat(document.mulform.m2.value))" />

</form>

<br />

Division:

<form name="divform" id="divform">

<input type="text" name="d1" />

/

<input type="text" name="d2" />

<input type="button" value="Divide!" onclick="divide(parseFloat(document.divform.d1.value), parseFloat(document.divform.d2.value))" />

</form>

</body>

</html>[/color]
Copy linkTweet thisAlerts:
@CrazyMerlinAug 26.2006 — hey, next time use a code block!
Copy linkTweet thisAlerts:
@CrazyMerlinAug 26.2006 — window.alert( (var1a*1) + (var2a*1)); ?????

anything multiplied by 1 never changes!

no need to put all your bits into separate script blocks!

you have no checking for integers....what if someone enters 'cat' and 'house'?

look up the parseInt() function

and use a code block next time!
Copy linkTweet thisAlerts:
@UltimaterNov 22.2006 — window.alert( (var1a*1) + (var2a*1)); ?????

anything multiplied by 1 never changes!

no need to put all your bits into separate script blocks!

you have no checking for integers....what if someone enters 'cat' and 'house'?

look up the parseInt() function

and use a code block next time![/QUOTE]

Multiplying by 1 changes a string-number into a number -- a shorthand for parseInt. Both alert("cat"*1) and alert(parseInt("cat")) alert NaN proving they are interchangeable. Muazzez's code is fine in that aspect.


<i>
</i>&lt;script type="text/javascript"&gt;
/* + is the addition operator as well as the string concentration operator */
alert("1"+"1")//11 (string detected, string concentration used)
alert("1"+1)//11 (string detected, string concentration used)
alert(1+1)//2
alert("cat"+1)//"cat1"
alert(NaN+1)//NaN

/* * is the multiplication operator */
alert("1"*"1")//1
alert("1"*1)//1
alert(1*1)//1
alert("cat"*1)//NaN
alert("cat"*"1")//NaN
alert("cat"*"dog")//NaN
alert(NaN*1)//NaN

/* converting string-numbers to numbers */
alert(parseInt("1")+1)//2
alert(("1"*1)+1)//2
alert(("1"/1)+1)//2
alert(("1"-0)+1)//2

/* bitwise operators */
alert("134.9999"&lt;&lt;0)//134 (decimal is dropped)
alert("134.9999"&gt;&gt;0)//134 (decimal is dropped)
alert("134.9999"&gt;&gt;&gt;0)//134 (decimal is dropped)
alert("134.9999"|0)//134 (decimal is dropped)
alert("134.9999"&amp;"134.9999")//134 (decimal is dropped)

//Could also use Number, Math.pow, eval, etc.... you get the idea, lol
&lt;/script&gt;
×

Success!

Help @AltF4 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.6,
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,
)...