Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Struggling with an element in my Javascript code

I am to make a script that will ask the user to enter numbers until they type quit. I have this working, but I am struggling with the final element where I have to calculate the number of entries (not sum) the user submitted not including the var - quit.

Any assistance would be greatly appreciated.

<script>
 

var userWord = "quit";
var numList = Array();
var total = 0;

do {

userWord = prompt ("Enter a number of the word quit");
userWord = userWord.toLowerCase();
numList.push(userWord);
}
while (userWord != "quit")

document.writeln("<p>");
for (t = 0; t < numList.length - 1; t++) {
document.write(numList[t]);
if (t < numList.length - 2) {
document.writeln(", ");
}

}
document.writeln("</p>");

</script>
 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There is an easier way. You can just use Array.prototype.join() to convert the array to a string with a delimiter of your choice.

Below is an example of this. Also note that document.write isn't commonly used. Instead we can just use a div element and write into it using the innerHTML property.

Final thing is the loop. Its up to you but I usually just loop forever and break when I receive the quit input. It can be cleaner but that's personal choice. Bear in mind that in your code you push the "quit" string into the result array, probably not what you want.

<html>
    <body>
        <div></div>
    </body>
    <script>

        const div = document.querySelector('div');
        const numList = [];
        
        while (true) {
        
            const userWord = prompt ("Enter a number of the word quit");
            if(userWord == null || userWord.toLowerCase() == 'quit') {
                break;
            }
            numList.push(parseInt(userWord, 10));
        }
        
        div.innerHTML = "<p>You typed " + numList.length + 
                        " numbers: " +  numList.join(', ') + "</p>";
    
    </script>
</html>

Checking that non integer input values are handled well by the code is an exercise left for the reader.

about 4 years ago · Juan Pablo Isaza Report

0

You can use numList.length-1 to count how many numbers were stored in the numList array, here is a working example:

 

var userWord = "quit";
var numList = Array();
var total = 0;

do {

userWord = prompt ("Enter a number or the word quit");
userWord = userWord.toLowerCase();
numList.push(userWord);
}
while (userWord != "quit")

document.writeln("<p>");
for (t = 0; t < numList.length - 1; t++) {
document.write(numList[t]);
if (t < numList.length - 2) {
document.writeln(", ");
}

}
document.writeln(`<br>You typed ${numList.length-1} number(s)`);
document.writeln("</p>");

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!