How to find the second largest?
const secondLargest = (arr) => {
let max = -Infinity, result = -Infinity;
for (const num of arr) {
if (num > max) {
[result, max] = [max, num]
} else if (num < max && num > result) {
result = num;
}
}
return result;
}
How to sort a string in JavaScript?
const sortString = (str) => {
return [...str].sort((a, b) => a.localeCompare(b)).join('');
}
or
const sortString = (str) => {
return text.split('').sort().join('');
}
How to create an array containing 1…N?
Array.from(Array(10).keys())
or
[...Array(10).keys()]