Code not working - from w3schools

Got this code off of w3schools and it isn’t working on my computer. Wrote it word for word. What is the problem?

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p>This example calls a function which performs a calculation and returns the result:</p>

<p id="demo"></p>

<script>
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {
    return a * b;
}
</script>

</body>
</html>

In what way do you believe that it is “not working”? The code is valid and when I copy it into a local file I see the expected results.

I don’t see “12” in my browser. :confused:

I don’t know what browser settings you may have that would prevent the script from working, but the code is valid:
image

I have two functions right now. The first one is this:


<p id="demo"></p>

<script>
function myFunction(p1, p2) {
    return p1 * p2;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>

The second one is what I posted above. Changed the function name and the id for the p tag. The first one works. The second doesn’t.

!?!I’m not sure, but maybe you are using an old browser?! why?!

try change <script> to <script type="text/javascript" language="javascript">, also move the function signature top of usage as following:

<script type="text/javascript" language="javascript">
function myFunction(a, b) {
    return a * b;
}
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
</script>

Also use html4 doctype, over html5. make sure there is no any extra char(including space) at the beginning of the file.
html4 doctype:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Also check if the file you created is in ASCII, check if it’s in UTF-8, so remove the BOM, and convert it to ASCII.

Please pal, explain and provide things clear.

I really don’t know what you’re talking about. The code in your original post works and it works if I replace the script tags with the one you included in your response.

Found the problem. Was a typo. Sorry guys. :slight_smile: