Refactor this loop to do more than one iteration

Here I am trying to find the four diagonally adjacent numbers in a 20*20 grid whose product is the greatest. Diagonals run from upper left to lower right. But something is wrong with the first loop: it does not iterate over the array. I could not find what it is.

function fouradjacentFinder(){
  let mystring = "08 02 22 97 38 15 ..."
   let  myarray  = mystring.split(" ").map(x =>Number(x));
    let counter ;
     let max = -Infinity;


 for(let i = 0;i<myarray.length;i++){
   counter= 0 ;

   for(let k =21;;k+21 ){
    myarray[i] = myarray[i]*myarray[k];
       counter++

     if(counter>4){
        break;
       }

   if(myarray[i]>max){
     max = myarray[i] ;
   }
   console.log(max)
  

 }
 return max;
 }
}

29 thoughts on “Refactor this loop to do more than one iteration”

Leave a Comment