Wednesday, November 18, 2015

Finding the missing number in JavaScript

A simple program that I wrote using JavaScript to find and determine what is the missing number in a given array list of numbers.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.






Sample Program Output

Program Listing

<html>
<head>
<title>Missing Number</title>
</head>
<style>
body {
  font-family:arial;
  font-size:16px;
  }
</style>
</head>
<body>
<script type="text/javascript">

function absent(arr){
    var mia= [], min= Math.min.apply('',arr), max= Math.max.apply('',arr);
    while(min<max){
        if(arr.indexOf(++min)== -1) mia.push(min);
    }
    return mia;
}
var arr = [27,30,23,20,26,22,25,29,21,24];


document.write("Missing number is: "+absent(arr));
</script>
</body>
</html>





No comments:

Post a Comment