const computeDash=(num)=>{
const arr=num.split('');
const nums=arr.map(Number);
for(let i=0;i<(nums.length-1);i++){
if(nums[i]%2===0 && nums[i+1]%2===0){
nums.splice(i,0,'-');
}
}
return nums.join('');
}
console.log(computeDash('025468'));
The above code is giving FATAL ERROR.
But the below code is working fine when I changed for loop. I can't understand why 1st code giving error. Does anyone know why?
const computeDash=(num)=>{
const arr=num.split('');
const nums=arr.map(Number);
for(let i=0;i<nums.length;i++){
if(nums[i-1]%2===0 && nums[i]%2===0){
nums.splice(i,0,'-');
}
}
return nums.join('');
}
console.log(computeDash('025468')); //0-254-6-8