GoonGamja
GoonGamja

Reputation: 2286

how do I insert dashes between each two even digits of a specific number

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

Upvotes: 1

Views: 7245

Answers (9)

Vikas Garg
Vikas Garg

Reputation: 44

  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(''))

Upvotes: 1

preet sarang
preet sarang

Reputation: 11

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 '));

Upvotes: 1

akhtarvahid
akhtarvahid

Reputation: 9779

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(''));

Upvotes: 0

Daut
Daut

Reputation: 2645

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

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386868

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'));

Upvotes: 0

Vikash_Singh
Vikash_Singh

Reputation: 1896

replace

numArr.push('-');

with

numArr.splice(i, 0, '-');

Upvotes: 1

protoproto
protoproto

Reputation: 2099

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'));

Upvotes: 0

memo
memo

Reputation: 1938

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

Upvotes: 3

Pardeep Dhingra
Pardeep Dhingra

Reputation: 3946

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'));

Upvotes: 0

Related Questions