0

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

3
  • 2
    Works fine for me: jsfiddle.net/tymeJV/E2KJD Commented Jun 6, 2013 at 15:06
  • 1
    What do you mean by "it keeps throwing off Dreamweaver"? This is valid syntax as far as I can tell. Commented Jun 6, 2013 at 15:06
  • 1
    Try document.write((10 / 2).toString(10) + "<br />");. If that works, we can say there's a bug in Dreamweaver... Actually it seems to have a bug already : ). Commented Jun 6, 2013 at 15:11

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

I did that but it's still giving me a syntax error in Dreamweaver
Stop using dreamweaver, then.
Either syntax is valid, honestly. Your IDE just sucks. If you create a brand new empty page in Dreamweaver and add that text to it, do you still get a syntax error?
As Chris says, even your original syntax is fine as is. This is better for clarifying to the human reader when writing the code, but both when interpreted give the same output. Dreamweaver is at fault here.
2
<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>

2 Comments

To declare another variable, like say I wanted to also do an addition problem would I 'var a=10+5; document.write(a + "<br />");'
You can declare one time and replace with new value. Or using the variable's array. Example: var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW";
0

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 />");

3 Comments

explain that serves ´.toString(10)´ please (excuse)
With Number.toString() method you can change a Number to String. 10 is a base you want to use in the result. For example 5.toString(2) gives you 101, which is 5 as binary.
@MirkoCianfarani You're wellcome. If you didn't ask, you wouldn't know it now : ).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.