Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda