Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

85
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda