What "might" I be doing wrong with this snippet?

Joined
Aug 16, 2022
Messages
52
Reaction score
2
Hi Everybody,
I'm just getting started with JavaScript but I'm frozen on this tiny example. What am, I doing wrong with it?

HTML:
<!DOCTYPE HTML>
<html>
    <head>
        <title>JavaScript Tests</title>
        <script src="playtime.js"></script>
    </head>
    <body onload="countToTen()">
        <h1>Lets count to TEN</h1>
        <p id="theCount"></p>
    </body>
</html>
Code:

JavaScript:
 function (countToTen) {
     var count = 0;
    while (count < 10 ) {
        count++;
        document.getElementById("theCount"). innerHTML += count +"<br>";
    }
 }

Both files, index.html and playtime.js are in the same directory, but the JavaScript code will not execute. It works fine as a single HTML file like this:
HTML:
OCTYPE HTML>
<html>
    <head>
        <title>JavaScript Tests</title>
    </head>
    <body onload="countToTen()">
        <h1>Lets count to TEN</h1>
        <p id="theCount"></p>
        <script>
             var count = 0;
            while (count < 10 ) {
               count++;
               document.getElementById("theCount"). innerHTML += count +"<br>";
             }
        </script>
    </body>
</html>
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
1.
[ on-line ]
HTML:
<html>
  <head>
    <title>JavaScript Tests</title>
    <script src="playtime.js"></script>
  </head>
  <body onload="countToTen()">
    <h1>Lets count to TEN</h1>
    <p id="theCount"></p>
  </body>
</html>
JavaScript:
function countToTen() {
  let count = 0;
  while (count < 10) {
    count++;
    document.getElementById("theCount"). innerHTML += count +"<br>";
  }
 }
or
JavaScript:
function countToTen() {
  let count = 0;
  while (count < 10)
    document.getElementById("theCount").innerHTML += (++count) +"<br>";
 }
or
JavaScript:
function countToTen(count=0) {
  while (count < 10)
    document.getElementById("theCount").innerHTML += (++count) +"<br>";
}


2.
[ on-line ]
HTML:
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Tests</title>
  </head>
  <body>
    <h1>Lets count to TEN</h1>
    <p id="theCount"></p>

    <script>
      window.onload = countToTen;

      function countToTen() {
        let count = 0;
        while (count < 10) {
          count++;
          document.getElementById("theCount").innerHTML += count +"<br>";
        }
      }
    </script>
  </body>
</html>



BTW, check
[ on-line ]
HTML:
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Tests</title>

    <style>
      pre {
        font: 400 1rem/1.05 system-ui, monospace;
      }
    </style>
  </head>
  <body>
    <h1>Lets count to TEN</h1>
    <pre id="theCount"></pre>

    <script>
      window.onload = () => {
        const intervalID = setInterval(() => {         
          document.getElementById('theCount').textContent += count + '\n';
          if (++count > 10) clearInterval(intervalID);
        }, 1000, count=1);
      };
    </script>
  </body>
</html>
 
Last edited:
Joined
Jul 4, 2023
Messages
366
Reaction score
41
function (countToTen) {
Here you have a mistake.

The code you've provided appears to be an anonymous JavaScript function declaration, but it lacks a proper function name. In JavaScript, functions should have a name or be assigned to a variable for later use or invocation.

JavaScript:
function countToTen() {
  // ...
}

In this corrected code, we've given the function a name, "countToTen," which allows you to call it later in your code using countToTen(). Without a function name, you won't be able to call or reference the function in your program, which is a common mistake.

or
JavaScript:
const countToTen = () => {
  // ...
}
 
Last edited:
Joined
Aug 16, 2022
Messages
52
Reaction score
2
Thank You Thank You Thank You Thank You.

I have been staring at my original code for over a week before I placed that inquiry on this forum. I just couldn't see what was wrong.

My eyes were crossed, my head pounded, my self-doubt was getting serious. Thank you for pointing out where the issue was. My fear of impending dementia is going away slowly.
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
My fear of impending dementia is going away slowly.
Paul, do not hesitate checking the browser Console for errors. This feature can be easily display by pressing F12. Doing this could save a week of your life at least 😉
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top