1

I am trying to insert dash(-) between two even numbers. Problem is dashes don't locate between two even numbers but at the end of the number.

here is the code

function insertHyphen(str) {
  var strArr = str.split('');
  var numArr = strArr.map(Number);
  for(var i = 0; i < numArr.length; i++) {
   if(numArr[i-1]%2===0 && numArr[i]%2===0) {
        numArr.push('-');
    }
  }
  return numArr.join('');
}
insertHyphen('112233445566'); // 112233445566---
GoonGamja
  • 1,936
  • 5
  • 20
  • 46

9 Answers9

3

Use splice() instead of push(). See here Splice MDN

function insertHyphen(str) {
  var strArr = str.split('');
  var numArr = strArr.map(Number);
    for(var i = 0; i < numArr.length; i++) {
      if(numArr[i-1]%2===0 && numArr[i]%2===0) {
        numArr.splice(i, 0, '-');
      }
    }
  return numArr.join('');
}
console.log(insertHyphen('112233445566')); // 112-2334-4556-6
Daut
  • 2,537
  • 1
  • 19
  • 32
3

Using regex:

var p = '112233445566';

var regex = /([02468])([02468])/g

console.log(p.replace(regex, '$1-$2'));

Try it online: https://tio.run/##y0osSyxOLsosKNHNy09J/f@/LLFIoUDBVkHd0NDIyNjYxMTU1MxM3ZqLCyRRlJqeWgGU1NeINjAyMbOI1YQz9NO5uJLz84rzc1L1cvLTNQr0ilILchKTUzXAmnQU1FUMdVWM1DU1rbn@/wcA

memo
  • 1,868
  • 11
  • 19
1

replace

numArr.push('-');

with

numArr.splice(i, 0, '-');
Vikash_Singh
  • 1,856
  • 2
  • 14
  • 27
1
function insertHyphen(str) {
  var strArr = str.split('');
  var numArr = strArr.map(Number);
    for(var i = 0; i < numArr.length; i++) {
      if(numArr[i-1]%2===0 && numArr[i]%2===0) {
        numArr.splice(i, 0, '-');
      }
    }
  return numArr.join('');
}
console.log(insertHyphen('025468 '));
1
  1. Convert number to String
  2. keep first element of array into separate array i.e. newArray
  3. Run the loop on string length
  4. Check current and the next element is divisible by 2 or not. If yes, push '-' else push the next item.
  5. Finally join the array elements of newArray with ''.

    let a = 224578;
    let str = a.toString()
    var newArray=[arr[0]]
    if(arr?.length > 0){
            
            for (let i = 0; i < arr.length; i++) {
                if(arr[i] % 2 === 0 && arr[i+1] % 2 === 0){
                    newArray.push('-', arr[i+1])
                } else{
                    newArray.push(arr[i+1])
                }
            }
        }

    console.log('newArray', newArray.join(''))
Vikas Garg
  • 44
  • 4
0

Push inserts element at the end of the array. You can use splice to enter element at particular position.

arr.splice(index, 0, item);

Here index is the position where you want to insert item element. And 0 represents without deleting 0 elements.

function insertHyphen(str) {
  var strArr = str.split('');
  var numArr = strArr.map(Number);
  for(var i = 0; i < numArr.length; i++) {
   if(numArr[i-1]%2===0 && numArr[i]%2===0) {
        numArr.splice(i, 0, '-');
    }
  }
  return numArr.join('');
}

console.log(insertHyphen('112233445566'));
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
0

A simple way:

function insertHyphen(str) {
  var strArr = str.split('');
  var numArr = strArr.map(Number);
  var result ="";
  for(var i = 0; i < numArr.length; i++) {
   if((numArr[i+1]!==undefined)&&(numArr[i]%2===0 && numArr[i+1]%2===0)) {
        //numArr.push('-');
        result = result + numArr[i] + "-";
    }else{
        result = result + numArr[i];
    }
  }
  return result;
}
console.log(insertHyphen('112233445566'));
protoproto
  • 2,081
  • 1
  • 13
  • 13
0

You could map the new string by check the value and predecessor.

function insertHyphen(string) {
    return Array
        .from(string, (v, i) => !i || v % 2 || string[i - 1] % 2 ? v : '-' + v)
        .join('');
}

console.log(insertHyphen('22112233445566'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Using push() method and single loop

let input = '346845';
let result = [input[0]], 
len = input.length;

for (var i = 1; i < len; i++) {
  if (input[i - 1] % 2 === 0 && input[i] % 2 === 0) {
    result.push('-', input[i]);
  } else {
    result.push(input[i]);
  }
}
console.log(result.join(''));
akhtarvahid
  • 9,445
  • 2
  • 26
  • 29