katiejallred
katiejallred

Reputation: 35

Divide with Javascript

I know that this has to be the simplest thing of all time, but I'm a beginner here. Why do I get a syntax error when I run

<script type="text/javascript" language="JavaScript">
document.write(10 / 2 + "<br />"); //Divide 10 by 5 to get 2
</script>

I know that "/" is the division symbol, but for some weird reason it keeps throwing off Dreamweaver

Upvotes: 0

Views: 267

Answers (3)

Teemu
Teemu

Reputation: 23416

Looks like Dreamweaver can't do automatic type casting when concatenating numbers to strings. Hence you need to do the type coercion yourself:

document.write((10 / 2).toString(10) + "<br />");

Upvotes: 0

Chris G.
Chris G.

Reputation: 3981

document.write( (10/2) + '<br>');

Upvotes: 4

Mirko Cianfarani
Mirko Cianfarani

Reputation: 2113

<script type="text/javascript" language="JavaScript">
//is better that add a variable
var p=10/5;
document.write(p + "<br />"); //Divide 10 by 5 to get 2
//or document.write(float(10/5) + "<br />"); //Divide 10 by 5 to get 2
</script>

Upvotes: 2

Related Questions