Q:

Write a JavaScript program to get the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order)

0

Write a JavaScript program to get the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
console.log(minN([1, 2, 3]));
console.log(minN([1, 2, 3], 2));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now