Iterative Rotation Cipher Kata code not passing

Hello fellow coders,

I’m a beginner coder working on my JS skills. I actually started solving some katas on CodeWars, super interesting! However there’s this one kata that I can’t pass and it’s frustrating because I feel like I’m super close.

It’s vailable here. It’s level 5 out of 8, but I’m really struggling to keep it clean and especially to get it to work.
I feel like my code is not catering well for all whitespaces and especially line break as it seems to be working fine otherwise.

const IterativeRotationCipher = {};

IterativeRotationCipher.encode = function(n,str){
  
  //Declare a function to store whitespace index. String as input. Array as output
  function storeWhiteSpace(str) {
    let whiteSpace = [];
    str.split('').forEach((item, index) => {
    if (/[ ]/g.test(item)) {
      whiteSpace.push(index);
      };
    });
    return whiteSpace;
  };

  //Create function to remove whitespace from string. String as input. String as output
  function removeSpace(str) {
     return str.replace(/[' ']/g,'')
  };

  //Create function to move each element in the array by ’n’ to the right. String as input. String as output
  function moveToRight(str) {
     let array = str.split('');
     let resultArray = [];
     array.forEach((item, index) => {
        index + n >= array.length? resultArray[(index + n)%array.length] = item : resultArray[index + n] = item;
  });
     return resultArray.join('');
  };
  
  //Create function to add back all spaces in place. String and the array containing whitespace index as inputs. String as output.
  function addSpaceBack(str, array) {
     let resultArray = str.split('');
     array.forEach(item => {
       resultArray.splice(item, 0, ' ');
       });
     return resultArray.join('');
  };

  //Create function to shift substring by 'n' place to the right. String as input and string as output.
  function moveSubstringToRight(str) {
    let array = str.split(' ');
    return array.map(item => moveToRight(item))
                .join(' ');
  };

  //Repeat the process ’n’ times
  const whiteSpaceArray = storeWhiteSpace(str);
  let i = n;
  let encodeString = str;
  while (i > 0) {
    encodeString = moveSubstringToRight(addSpaceBack(moveToRight(removeSpace(encodeString)),whiteSpaceArray));
    i--;
  }
  return n + ' ' + encodeString;
};

IterativeRotationCipher.decode = function(str){

//Identify the n
let n = str.split(' ')[0];

//Declare a function to remove the code at the beginning. String as input. String as output
function removeCode(str) {
   let resultArray = str.split(' ')
   resultArray.shift()
   return resultArray.join(' ');
}

//Declare a function to store whitespace index. String as input. Array as output
function storeWhiteSpace(str) {
   let whiteSpace = [];
   str.split('').forEach((item, index) => {
      if (/[' ']/g.test(item)) {
   whiteSpace.push(index);
   };
});
return whiteSpace;
};


//Create function to remove whitespace from string. String as input and string as output
function removeSpace(str) {
   return str.replace(/[' ']/g,'')
};

//Create function to move each element in the array by ’n’ to the left. String as input. String as output
function moveToLeft(str) {
   let array = str.split('');
   let resultArray = [];
   array.forEach((item, index) => {
      if (index - n >= 0) {
   resultArray[(index - n)] = item
}
      else if ((index - n)%array.length == 0) {
   resultArray[0] = item;
}
else {
   resultArray[array.length + (index - n)%array.length] = item;
}
});
return resultArray.join('');
};

//Create function to add back all spaces in place. String and the array containing whitespace index as inputs. String as output
function addSpaceBack(str, whiteSpaceArray) {
   let resultArray = str.split('');
   whiteSpaceArray.forEach(item => {
      resultArray.splice(item, 0, ' ');
   });
   return resultArray.join('');
};

//Create function to shift substring by 'n' place to the right. String as input. String as output.
function moveSubstringToLeft(str) {
   let array = str.split(' ');
   return array.map(item => moveToLeft(item))
                              .join(' ');
};

//Repeat the process ’n’ times
const whiteSpaceArray = storeWhiteSpace(removeCode(str));
let i = n;
let decodeString = str.split(' ').slice(1).join(' ');
while (i > 0) {
   decodeString = addSpaceBack(moveToLeft(removeSpace(moveSubstringToLeft(decodeString))),whiteSpaceArray);
i--;
}
return decodeString;
};

I know it’s pretty long and might be painful to review, but in case you took the challenge or have good practices to advise, I’ll be super thankful. I think next step would be to focus on getting partners for pair-programming as that seems a dope way to get better!!

Happy coding!!