0

I have been trying to get this relatively simple code to run in a web browser where it just prints out the answers to a handful of math problems. The code I have should work because I checked on w3schools and it is basically the same code except for the variables I added in it. Here is my code below for the HTML and JS files. I have the rest of the code in JavaScript commented out just to see if it'll work for the one I am trying to run first but everything looks like it checks out to me but I guess I am wrong. Please let me know what I am doing wrong and how to fix it.

    var x = "9";
var y = "9";
var z = x * y;   
document.getElementById("demo").innerHTML = z;

//let b = 56 / 12;
//console.log(b);

//let c = 281 % 9;
//console.log(c);

//let d = 18 / 0;
//console.log(d);

//let e = '56' + '92';
//console.log(e);

//let f = 42 == 52;
//console.log(f);
    <!DOCTYPE js>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Activity 1</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="Activity-1.js"></script>
    </head>
    <body>
    
    <p id="demo"></p>
    
        <script>
          
        </script>

    </body>
</html>
Tony
  • 3
  • 1
  • 1
    I just added your code to a Stack Snippet so we can see the result here, and it seems to work. – Joseph Sible-Reinstate Monica Apr 20 '20 at 23:12
  • Do you have any idea why it isn't working on my end then? I am using VScode and it's been working just fine for me but just isn't running this for some reason. – Tony Apr 20 '20 at 23:16
  • 1
    @JosephSible-ReinstateMonica that's because the _script_ part of a Stack Snippet runs **after** the document has finished loading – Phil Apr 20 '20 at 23:23

1 Answers1

0

Try moving your <script src="Activity-1.js"></script> tag to the bottom of the Body section, just above the closing </body> tag. This might help, something similar has bitten me before.

Nik P
  • 2,693
  • 9
  • 21
  • That worked! Thank you! but now since it was working I started to add the same formatting for the rest of the equations and checked to see the result and it only prints out one. Is there a way I should go about doing it to be able to see all of the answers at once? – Tony Apr 20 '20 at 23:23
  • Using the same markup, you are just changing the same ```

    ``` element every time. for the rest you need to append an element for each calculation. See [this page](https://www.w3schools.com/jsref/met_document_createelement.asp) for more details.

    – Nik P Apr 20 '20 at 23:28