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;
}