Piotr Berebecki Piotr Berebecki - 5 months ago 7
Javascript Question

Use reduce() method to find a skipped number in an array of consecutive numbers

I'm trying to use the

reduce()
method to find one skipped (missing) number in an array of (sometimes almost) consecutive numbers. Only one number will be missing at most.
This is my codepen: http://codepen.io/PiotrBerebecki/pen/zBrRVd

For example,

findMissing([1,2,3,5])
should return 4



findMissing([1,2,3,4])
should return
undefined




findMissing([2,3,4,6])
should return 5

The code that I developed seems to work fine if there is indeed a number that was skipped. But it returns an undesired value if all numbers are present. Would you know how to fix it?

My JS code:

function findMissing(arr) {
return arr.reduce(function(prev, curr) {
if (curr - prev !== 1) {
return curr - 1;
}
});
}


// This should return 4, and it indeed returns 4
console.log( findMissing([1,2,3,5]) );


// This should return 'undefined', but it returns 3
console.log( findMissing([1,2,3,4]) );


// This should return 5, and it indeed returns 5
console.log( findMissing([2,3,4,6]) );


UPDATE 1:

Based on the answers below, the following code delivers the desired outcome using the
reduce()
method:

// ****SOLUTION:****
function findMissing2(arr) {
return arr.reduce(function(prev, curr, index) {
if (curr === arr[0] + index) {
return prev;
} else {
return arr[0] + index;
}
}, void 0);
}

console.log( findMissing2([1,2,3,4]) ); // Undefined
console.log( findMissing2([1,2,3,5]) ); // 4
console.log( findMissing3([2,3,4,6]) ); // 5
console.log( findMissing2([2,3,4,5]) ); // Undefined

Answer

I would do this job as follows;

var a1 = [1,2,3,5],
    a2 = [1,2,3,4],
  res1 = a1.reduce((p,c,i) => c == i+1 ? p:i+1,void 0),
  res2 = a2.reduce((p,c,i) => c == i+1 ? p:i+1,void 0);
console.log(res1);
console.log(res2);

Note: void 0 is a very safe undefined value in JS.