Higher order array functions and methods

Hi guys, I wrote this program, which prompts an input of a number, and inserts dashes(-) between even numbers. Example: input: 025468, output: 0-254-6-8. It works, but I will like to find out if there’s a way to use a .map() function or any other higher order array method in place of the for loop. The challenge is that the iteration starts from 1 not 0.

function evenDash() {
  let numberArray = prompt("Enter a 
  number").split("").map((item) => +item); 
  let L = numberArray.length; 
  let result = []; 
  result.push(numberArray[0]); 
    
  for (let i = 1; i < L; i++) { 
    if (numberArray[i] % 2 === 0 && 
      numberArray[i - 1] % 2 === 0) {
      result.push("-", numberArray[i]);
      } else {
      result.push(numberArray[i]);
  } 
}
let stringResult = result.map((item) => 
String(item)).join(""); 
return stringResult;
}

Hello there,

Just as a hint, the map method can return the index as the second parameter…perhaps some logic to check which side of the indices you are on…

Also, perhaps think about using regex, instead of looping through (would be more performative)

Hope this helps.

Thanks for your your help. I’ll try using the index with some logic to start iteration from 1…
I don’t know regex yet…I’ll check it out…

Thanks again