Skip to content

Commit

Permalink
another solution for problem llipio#18
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalra247 committed Jun 20, 2017
1 parent 4b177b3 commit 4aea3e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
48 changes: 35 additions & 13 deletions solutions/18.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Rahul Kalra
// Return the missing number in an input array of consecutively increasing number

const solution = (array) =>{
let newArray = [];
for(let i = 0; i < array.length; i++){
if(!((array[i + 1]) - (array[i])) === 1){
newArray.push((array[i]) + 1);
}
}
return newArray;
};

module.exports = {solution};


// Find missing number in array
// Return the missing number in an input array of consecutively
// increasing numbers.
Expand All @@ -14,8 +30,10 @@
* input array or
* null if none is found
*/
const solution1 = (arr) => {
let missingNum = null;

/*const solution1 = arr => {
let missingNum = null,
>>>>>>> another solution for problem #18
i = 0;
while (!missingNum && i < arr.length - 1) {
Expand All @@ -37,17 +55,21 @@ const solution1 = (arr) => {
* the input array or
* null if none is found
*/
const solution2 = (arr) => {
let i = 1;

while (i < arr.length && arr[i - 1] + 1 === arr[i]) {
i++;
}
// const solution2 = arr => {
// let i = 1;
//
// while (i < arr.length && arr[i - 1] + 1 === arr[i]) {
// i++;
// }
//
// return i === arr.length ? null : arr[i - 1] + 1;
//}

return i === arr.length ? null : arr[i - 1] + 1;
};

module.exports = {
solution1,
solution2,
};

//module.exports = {
// solution1,
// solution2
//};

5 changes: 3 additions & 2 deletions test/18.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const expect = require('chai').expect;
let solution1 = require('../solutions/18').solution1;
solution2 = require('../solutions/18').solution2;
// solution = require('../yourSolution').solution;

describe('Find missing number in array', () => {
it('should return 7 for [5,6,8,9]', () => {
const result1 = solution1([5, 6, 8, 9]);
result2 = solution2([5, 6, 8, 9]);
expect(result1).to.equal(7);
expect(result2).to.equal(7);
});

});

it('should return 94 for [89,90,91,92,93,95]', () => {
const result1 = solution1([89, 90, 91, 92, 93, 95]);
Expand All @@ -32,3 +32,4 @@ describe('Find missing number in array', () => {
expect(result2).to.equal(-8);
});
});

0 comments on commit 4aea3e7

Please sign in to comment.