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

211
Visualizações
How to loop through an array and display user input with indexes?

How do i loop through an array and display user input with indexes without the user input replicating?

Current output i'm getting:

Feedback 1

123456

Feedback 2

123456

The expected output for the case below should be:

Feedback 1

123

Feedback 2

456

<!DOCTYPE html>
<html>
    <head>
    <script src="script.js" type="text/javascript"> </script>
    </head>

    <body> 
    
    <h2>Feedback Form</h2><br>

    <form>
        Enter Feedback : <textarea rows="3" cols="20" id="feedback"></textarea><br><br>
        <input type="button" value="Submit Feedback" id="create" onclick="addFeedback()"><br><br>
        <input type="button" value="View Feedback" id="view" onclick="displayFeedback()"><br><br>
    </form>

    <div id="result"></div>
    </body>
</html>
var myArray = [];

var myFeedback = document.getElementById("feedback");
var displayBox = document.getElementById("result");

function addFeedback(){
    

    //Store feedback into array
    myArray.push(myFeedback.value);

    //Clear textbox
    myFeedback.value = "";

    //Display message
    displayBox.innerHTML = "Successfully Added Feedback Details!";

    }   
   
   function displayFeedback(){
    
    displayBox.innerHTML = "";

    for(var i = 0; i < myArray.length; i++){
    displayBox.innerHTML += "Feedback " + (i+1) + "<br/>" + myArray.join();
    }

}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use myArray[i] instead of join. Or more modern approach:

let innerHTML = "<ul>";

myArray.forEach((value, index) => {
    innerHTML += `<li>Feedback ${index+1}: <br /> ${value}</li>`; 
});

innerHTML += "</ul>";

displayBox.innerHTML = innerHTML;
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