Back to questions
JuniorTheoryCommon
What does the program print?
const nums = [10, 9, 1];
nums.sort();
console.log(nums.join(" "));1 10 9
- Expecting
1 9 10: without a functionsortcompares elements as strings, and the string"10"comes before"9"because"1"is less than"9". - Expecting
10 9 1:sortchanges the array it is called on, sonumsitself is sorted; onlytoSortedleaves the original alone.
- Which function would you pass to
sortto order the numbers as1 9 10? - How would you get a sorted copy and keep
numsunchanged?