2

I have the following two arrays, which is always pre-sorted:

[1,2,4,5,8]
[x,x,x,x,x]

I need to insert the missing elements and in corresponding array, put y so the output can be:

[1,2,3,4,5,6,7,8]
[x,x,y,x,x,y,y,x]

The data arrives separately but they always match size wise.

I have tried the following but am pretty sure i have over-complicated it.

function action(numbers,data){
  var len=numbers.length;
  if (len<=1){
    return [numbers,data];
  }
  var new_data=[] //stores new data
  var new_number=[] //stores new numbers
  for(var i=1;i<len;i++){
    var diff=numbers[i] - numbers[i-1];

    if(diff>1){
      //there is gap here
      var val=0;
      diff--;

      for(var j=0;j<diff;j++){
        val=numbers[i-1] + j +1;
        new_number.push(val)
        new_data.push('y')
      }
      //put current info after missing data was inserted
      new_number.push(numbers[i])
      new_data.push(data[i])
    }
  }

  //adjust first entry
  new_number.unshift(numbers[0])
  new_data.unshift(data[0])

  return [new_number,new_data];
}

It is not consistent and I myself can't follow it.

action([2002,2005,2007],['x','x','x']) =>[2002,2003,2004,2005,2006,2007], [x,y,y,x,y,x]

But the following is error:

action([2002,2003,2007],['x','x','x']) =>[2002,2004,2005,2006,2007], [x,y,y,y,x]

the output should have been 2002,2003,2004,2005,2006,2007 and x,x,y,y,y,x

Update

Adding an else to the diff>1 seems to fix the above errors but the solution is not elegant at all:

} else{
  new_number.push(numbers[i])
  new_data.push(data[i])
}
2
  • Can you give examples of inconsistencies? Commented Jun 28, 2018 at 16:13
  • Check @GCadogan I have added a couple of examples Commented Jun 28, 2018 at 16:23

4 Answers 4

2

You were so close. I only had to move a few lines. The original numbers needed pushed to the new array every time, not just when the difference is too large.

function tst(numbers, data){	
    var len=numbers.length;
    if (len<=1){
        return [numbers,data];
    }
    var new_data=[] //stores new data
    var new_number=[] //stores new numbers
    for(var i=1;i<len;i++){
        var diff=numbers[i] - numbers[i-1];

        if(diff>1){
            //there is gap here
            var val=0;
            diff--;

            for(var j=0;j<diff;j++){
                val=numbers[i-1] + j +1;
                new_number.push(val)
                new_data.push('y')
            }
        }
		
        //put current info after missing data was inserted
        new_number.push(numbers[i])
        new_data.push(data[i])
    }

    //adjust first entry
    new_number.unshift(numbers[0])
    new_data.unshift(data[0])

    return [new_number,new_data];
}
	
console.log(tst([1,2,4,5,8],['x','x','x','x','x']));

Sign up to request clarification or add additional context in comments.

Comments

2

You could use while loop and start from last element and finish at first.

var a = [1, 2, 4, 5, 8]
var b = ["x", "x", "x", "x", "x"];
var i = a[a.length - 1];

while (i-- > a[0]) {
  if (!a.includes(i)) {
    var index = a.indexOf(i + 1)
    a.splice(index, 0, i);
    b.splice(index, 0, "y");
  }
}

console.log(a);
console.log(b);

1 Comment

Thanks @Nenad Vracar. That's much readable than mine and does output the expected output. Lemme check for speed only. Thanks a bunch.
1

You could use forEach:

const demo = (numbers,xy)=>{
  let expected = numbers[0],retNumbers=[],retXy=[];
  numbers.forEach(
    (current,index)=>{
      while(current>expected){
        retNumbers.push(expected);
        retXy.push("y");
        expected++
      }
      retNumbers.push(current);
      retXy.push(xy[index]);
      expected++;
    }
  );
  return [retNumbers,retXy];
}

console.log(demo(
  [1,2,4,5,8],
  ["a","b","c","d","e"]
))

1 Comment

Thanks @HMR That's awesome too.
0

Hope this helps. Worked-out with dummy arrays.

var a = [1,4,6,7,8,12,14,55];
var b = [2,3,5,9,13,25,27,54,56];
b.forEach(function(value){
    var i = a.findIndex(function(oldvalue){return oldvalue>value;});
    i==-1 ? a.push(value) : a.splice(i,0,value);;
});
console.log(a)

1 Comment

Thanks but the output is not as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.