The whiteboard problem is: find shortest string in an array and then shorten all of the other strings to that length starting at index 0
I use sorting to find the shortest string. (not best practice but hey i'm learning). But when I try to map the arr
later, it remains sorted, even if I set it to a different variable early in the function. Is this an issue with let
? should I be using const
instead?
my unsorted.map
is returning a sorted array. I have no idea why.
Here is my solution:
function cutIt(arr){
let unsorted = arr;
console.log(unsorted)
let sortedArr = arr.sort((a, b) => a.length - b.length);
let shortest = sortedArr[0].length;
console.log(unsorted)
return unsorted.map(str => str.slice(0, shortest))
}