Sorts Names - Javascript

Asked

Viewed 629 times

1

I need to create an HTML document where the user can add names to a numbered list (OL). Your document must have a text box (an input for typing text) where the user can enter one name at a time. In addition, it should have a button labeled "Add", which when clicked adds the typed text to the OL list. The user can add as many names as he wants and each name is added at the end of the OL list. However, at any time of execution, the user can sort the names of the OL list by clicking on a button labeled "Sort List". The user click on the "Sort List" button, it should be regrouped in an orderly manner. Use the sorting algorithm of your choice (Bubble, Insertion, Quick, etc).

z códico it at the time of sorting I think identifies the escaço as another variable and divides.

<!doctype HTML>
<html>
<head>
    <title>Eventos em javascript</title>
    <meta charset="utf-8" >
    <script>
        var arr = [];
        function adicionar(){

    var nome = document.getElementById("nome");

    lista.innerHTML = lista.innerHTML + "<li>" + nome.value + "</li>";
    arr = arr + nome.value + " ";
}
    function ordenar(){
    document.getElementById("lista").innerHTML = "";
    var iv = arr.split(" ");
    iv.pop();
    iv.sort();
    for (var i = 0 ; i < iv.length ; i++) 
    {
        lista.innerHTML = lista.innerHTML + "<li>" + iv + "</li>";
    }
}
</script>
</head>
<body>
<header>
    <h3>Lista de Nomes</h3>    
</header>
<input id="nome" type="text">
<button onclick="adicionar()">Adicionar</button>
<ol id="lista">
</ol>
<button onclick="ordenar()">Listar por ordem alfabética</button>
<div class="col-sm-10">

</div>
</body>
</html>
    • See if this helps you: 1) https://answall.com/questions/120801/como-fazer-listagem-ordenada-por-letra 2) https://answall.com/questions/148097/comor-fazer-lista-ordenada 3) https://pt.overfstacklow.com/questions/8136/como-crio-um-sum%C3%A1rio-no-text-with-some-wysiwyg

1 answer

0

Just change that line

lista.innerHTML = lista.innerHTML + "<li>" + iv + "</li>";

for that

lista.innerHTML = lista.innerHTML + "<li>" + iv[i] + "</li>";

to traverse (iterate) the array already ordered iv.sort(); and print the result

var arr = [];
function adicionar(){
   var nome = document.getElementById("nome");
   lista.innerHTML = lista.innerHTML + "<li>" + nome.value + "</li>";
   arr = arr + nome.value + " ";
}

function ordenar(){
   document.getElementById("lista").innerHTML = "";
   var iv = arr.split(" ");
   iv.pop();
   iv.sort();
   for (var i = 0 ; i < iv.length ; i++) 
   {   
     lista.innerHTML = lista.innerHTML + "<li>" + iv[i] + "</li>";
   }
}
<header>
    <h3>Lista de Nomes</h3>    
</header>
<input id="nome" type="text">
<button onclick="adicionar()">Adicionar</button>
<ol id="lista">
</ol>
<button onclick="ordenar()">Listar por ordem alfabética</button>
<div class="col-sm-10">

</div>

  • Please read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Browser other questions tagged

You are not signed in. Login or sign up in order to post.