0
0
DSA Javascriptprogramming~10 mins

Why Sorting Matters and How It Unlocks Other Algorithms in DSA Javascript - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sort the array in ascending order using the built-in method.

DSA Javascript
const numbers = [5, 3, 8, 1];
numbers.sort([1]);
console.log(numbers);
Drag options to blanks, or click blank then click option'
A() => a - b
B(a, b) => b - a
C(a, b) => a - b
D() => b - a
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function without parameters causes incorrect sorting.
Using (a, b) => b - a sorts in descending order, not ascending.
2fill in blank
medium

Complete the code to find the median after sorting the array.

DSA Javascript
const arr = [7, 2, 5, 3];
arr.sort([1]);
const mid = Math.floor(arr.length / 2);
const median = arr.length % 2 !== 0 ? arr[mid] : (arr[mid - 1] + arr[mid]) / 2;
console.log(median);
Drag options to blanks, or click blank then click option'
A(a, b) => a - b
B() => a - b
C(a, b) => b - a
D() => b - a
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting descending changes the median calculation.
Not providing a comparison function sorts elements as strings.
3fill in blank
hard

Fix the error in the code to correctly sort an array of strings by length.

DSA Javascript
const words = ['apple', 'dog', 'banana', 'cat'];
words.sort((a, b) => [1]);
console.log(words);
Drag options to blanks, or click blank then click option'
Ab - a
Bb.length - a.length
Ca - b
Da.length - b.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using a - b or b - a compares strings incorrectly.
Sorting descending when ascending is needed.
4fill in blank
hard

Fill both blanks to create a sorted array of unique numbers.

DSA Javascript
const nums = [4, 2, 5, 2, 3, 4];
const uniqueSorted = [...new Set(nums)].sort([1]);
console.log(uniqueSorted);
Drag options to blanks, or click blank then click option'
A(a, b) => b - a
B(a, b) => a - b
C() => a - b
D() => b - a
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a comparison function sorts numbers as strings.
Using arrow functions without parameters causes errors.
5fill in blank
hard

Fill all three blanks to create a sorted array of objects by age, then map to names.

DSA Javascript
const people = [
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 25 },
  { name: 'Carol', age: 35 }
];
const sortedNames = people.sort((a, b) => [1]).map(person => person.[2]).filter(name => name.length > [3]);
console.log(sortedNames);
Drag options to blanks, or click blank then click option'
Aa.age - b.age
Bname
C3
Db.age - a.age
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting descending when ascending is needed.
Mapping to wrong property name.
Filtering with incorrect length value.