Hello everyone ! I’m a beginner in JS and i recently stumbled upon a problem i’ve never actually had before :
I was doing an algorith challange that consited in finding the X biggest string in an array and return them in their order of appearance . My first guest in solving this was to create a copy of one of the array and sort it by length, this way i could determine the X longest elements, look for them in the orignial array and then return them.
So i wrote a code that looked like this :
longestConsec(strarr,k) {
biggest = strarr;
biggest.sort(function(a,b){
return b.length-a.length
});
}
but at this point I realised that strarr and biggest were somehow bonded, and that sorting one would automatically sort the other, as for splitting, slicing, etc…
I guess i could figure another way around, but I’d like to know as much as possible in case i’d stumbled upon a problem where i’d need both the original argument and a cloned one that I could mess around with.
So, would you happen to know anyway to make a copy of an arguments inside a function that would work independently of the original ? Thanks a bunch !