Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!