0

I'm trying to print text and the defined variables using the document.write function in Notepad++ and JavaScript. I can't get anything to show up in my web browser when I open the .html file. I'm new to JavaScript. Here's my code.

<html>

<body>
    <script> var x == 23 ; var y == 55 ; var z == var x + var y ;</script>
    <script> document.write("The sum of x + y" + z +<br>);</script>
    <script> document.write("The sum of x + y = " + z + <br>);</script>
    <script> document.write("The sum of x + y = " + ( x + y) + <br>);</script>
    <script> var x = "Bob Dylan" , var y = "is enrolled in COP 2500" , var z = "with Professor Whiting, she is the BEST!"</script>
    <script> document.write( x + y + z);</script>
</body>

</html>
1
  • 1
    You should take a look into your browsers developers tools. In most browsers you can access them using F12. Open the console there and reload the page. It will tell you when you get the JavaScript Syntax wrong. Commented May 29, 2015 at 18:49

2 Answers 2

2
<html>
    <body>
        <script>
            var x = 23 ; var y = 55 ; var z = x + y ;
            document.write("The sum of x + y = " + z + '<br>');
            document.write("The sum of x + y = " + z + '<br>');
            document.write("The sum of x + y = " + ( x + y) + '<br>');
            x = "Bob Dylan" , y = "is enrolled in COP 2500" , z = "with Professor Whiting, she is the BEST!"
            document.write( x + y + z);
        </script>
    </body>
</html>

Ok, let's go through the changes I made here. First off, blocks of script only need an opening <script> tag, and a closing </script> tag. You don't need to create a new script block for every line.

Next, when assigning a value to a variable, only use one equals sign. == is a comparison operator.

Next, any HTML you are writing to the page from inside a script tag, needs to be in quotes: '<br />'

Finally, once a variable has been created, you don't need to re-initialize with var again. For instance, I create a variable using var x = 0. Then I can use that variable later by simply saying x;.

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

Comments

0

When you're testing code like that, modern browsers come with a JavaScript console that shows you if there are errors in your code. Usually you just open up your test page and press F12 to see what's going on.

In your case, the first problem comes from the way you're defining x, y, and z. Instead of setting them equal to something, you're asking if they are equal. So, var x = 23 says that x now refers to 23, while x == 23 returns a true or false statement for whether x does equal 23 or not.

You've got another issue with defining z. var is used to define a variable to a certain scope (in this case global scope). It's not used when calling that variable after it's already defined. Later, when you're declaring the variables again, you don't need to use var because you are just re-assigning those variables.

One last issue you have is not enclosing your HTML in quotation marks. JavaScript sees the HTML <> tags and gets confused.

<html>
<body>
<script> var x = 23 ; var y = 55 ; var z = x + y ;
document.write("The sum of x + y" + z +"<br>");
document.write("The sum of x + y = " + z + "<br>");
document.write("The sum of x + y = " + ( x + y) + "<br>");
x = "Bob Dylan", y = "is enrolled in COP 2500", z = "with Professor Whiting, she is the BEST!"
document.write( x + y + z);</script>
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.