It only works where both of the lengths of the arrays are equal. Otherwise, it adds cute 'undefined' in the end. Also, these if checks are there for safety. They may be ugly and repetitive but I did not want the calculate every other case.
constsnail=input=>{letfinalResult=[];while(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){consta=input.length;//take first line in wholeif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){result=[...input.shift()];}//take end of each lineif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){for(letindex=0;index<a-1;index++){constelement=input[index].pop();result.push(element);}}//take last line in reverseif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){result=[...result,...input.pop().reverse()];}//take start of each middle lineif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){for(letindex=a-3;index>-1;index--){constelement=input[index].shift();result.push(element);}}//add this loop's result to finalResultfinalResult=[...finalResult,...result];}console.log(finalResult);};
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It only works where both of the lengths of the arrays are equal. Otherwise, it adds cute 'undefined' in the end. Also, these if checks are there for safety. They may be ugly and repetitive but I did not want the calculate every other case.