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

199
Visualizações
In this context what is the purpose of 'alert(strings)'

Question a: (i) Write a JavaScript code fragment that obtains four strings from text fields, stores them in an array, uses the sort function to sort it into ascending lexicographical order, and displays the sorted array in a presentation tag on the HTML page from which the script was invoked. All interactions with the document object model should be done using JQuery. You should assume that the input fields have id attributes id1, id2, id3, and id4, and the presentation tag has the id attribute sorted

Solution I found: 

$(document).ready(function(){
    var strings =[$("#id1").val(),$("#id2").val(),$("#id3").val(),$("#id4").val()];
    strings.sort();
    $("#sorted").text(strings);
    alert(strings); 
});

I have a question about that solution: What is the alert(strings); line for? I know what the alert function does but I don't see the question asking for it.

(ii) Describe how the code produced for part (a) would need to be modified to sort numbers into ascending numerical order

It seems to be the code above already sorts numbers into ascending numerical order, am I wrong?

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

0

What is the alert(strings); line for?

Just to show the result.

It seems to be the code above already sorts numbers into ascending numerical order, am I wrong?

That's incorrect. If you don't provide a callback, sort defaults to a lexicographic sort (loosely speaking, it sorts alphabetically), converting the elements to strings (for the sort comparison only) if necessary:

const a = [3, 10, 7, 20];
a.sort();
console.log(a);

Notice how 10 comes before 3, because the string "10" sorts before the string "3" in a lexicographic sort, because "1" is before "3".

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