# JavaScript: Missing number from an array

## JavaScript Math: Exercise-83 with Solution

Write a JavaScript program to find the missing number in a given array. There are no duplicates in the list.

Test Data:
([1,2,3,5,6,7]) -> 4
([2,3,4,5]) -> 1

Sample Solution:

HTML Code:

``````<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript program to missing number from an array</title>
<body>

</body>
</html>
```
```

JavaScript Code:

``````function test(nums) {
for (let n = 1; n<= nums.length + 1; n++) {
if (nums.indexOf(n) === -1)
return n;
}
}

nums = [1,2,3,5,6,7]
console.log("nums = "+nums)
console.log("Missing number of the said array: "+test(nums));
nums = [2,3,4,5]
console.log("nums= "+nums)
console.log("Missing number of the said array: "+test(nums));
```
```

Sample Output:

```nums = 1,2,3,5,6,7
Missing number of the said array: 4
nums= 2,3,4,5
Missing number of the said array: 1
```

Flowchart:

Live Demo:

See the Pen javascript-math-exercise-83 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿