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

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

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 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