+ 3

How to make better this 'cut string array' function?

Hi there! I'd like to see better ways to make the code below... https://code.sololearn.com/WRvEB2Js1aOD/?ref=app function cutIt(){ strArray = prompt("Write a String Array comma separated please"); if(strArray){ strArray = strArray.split(","); let strMinLength = strArray[0].length; strArray.forEach(element => { if(element.length < strMinLength ){ strMinLength = element.length } }); let choppedArr = []; strArray.forEach( el => { choppedArr.push( el.slice( 0,strMinLength )) }); alert("Minimum strings's length is " + strMinLength + ", so the new chopped array is " + choppedArr ); } }

14th Aug 2019, 1:47 AM
Yoel
Yoel - avatar
1 Answer
+ 3
I really liked the way how they are teaching JavaScript at codewars. I couldn't find much improvement other than doing trimming and finding minStrLength in same loop. There are Scopes of few performance improvement by using for loop instead of forEach. I am curious to see other alternatives as well. https://code.sololearn.com/WkkSntZAm1As/?ref=app
2nd Sep 2019, 12:06 PM
Morpheus
Morpheus - avatar