AlgoDaily Solution

1var assert = require('assert');
2
3function missingNumbers(numArr) {
4  let missing = [];
5
6  for (let i = 1; i < numArr.length; i++) {
7    // check where there are gaps
8    if (numArr[i] - numArr[i - 1] != 1) {
9      let x = numArr[i] - numArr[i - 1];
10      let diff = 1;
11      while (diff < x) {
12        missing.push(numArr[i - 1] + diff);
13        diff++;
14      }
15    }
16  }
17
18  return missing;
19}
20
21try {
22  assert.deepEqual(missingNumbers([0, 1, 3]), [2]);
23
24  console.log('PASSED: ' + 'missingNumbers([0, 1, 3]) should equal [2]');
25} catch (err) {
26  console.log(err);
27}
28
29try {
30  assert.deepEqual(missingNumbers([10, 11, 14, 17]), [12, 13, 15, 16]);
31
32  console.log(
33    'PASSED: missingNumbers([10, 11, 14, 17]) should equal [ 12, 13, 15, 16 ]'
34  );
35} catch (err) {
36  console.log(err);
37}
38
39try {
40  assert.deepEqual(
41    missingNumbers([3, 7, 9, 19]),
42    [4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18]
43  );
44
45  console.log(
46    'PASSED: missingNumbers([3, 7, 9, 19]) should equal [ 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18 ]'
47  );
48} catch (err) {
49  console.log(err);
50}

Community Solutions

Community solutions are only available for premium users.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.

JAVASCRIPT
OUTPUT
Results will appear here.