Mads Hjorth
Mads Hjorth

Reputation: 447

Looping multiple if statements based on array length

Just started learning javascript.

Input could be something like.

1, 5, 2, 7

and my task is to figure out how many numbers is missing between the lowest number and highest number.

var sorted = statues.sort();
var ticker = 0;
var plusser = sorted[0] + 1;
var plusser1 = sorted[1] + 1;
var plusser2 = sorted[2] + 1;
var plusser3 = sorted[3] + 1;


if(sorted[1] != plusser) {
  ticker++
}
if(sorted[2] != plusser1) {
  ticker ++;
}
if(sorted[3] != plusser2) {
  ticker ++;
}
if(sorted[4] != plusser3) {
  ticker ++;
}

this works great if there only is 4 numbers of input however, that ain't always the case and i am sure this can be coded cleaner if you use some sort of loop. Can you guys help me?

Upvotes: 0

Views: 218

Answers (4)

Ori Drori
Ori Drori

Reputation: 191976

If the array contains unique numbers (ie - 5 can't appear twice), you can use simple math:

var statues = [1, 5, 2, 7];

var result = 
  Math.max.apply(Math, statues) - Math.min.apply(Math, statues)  + 1 // the amount of items that should be in the array
  - 
  statues.length; // the current amount of items

console.log(result);

If you want the numbers as well, create a map of the existing numbers, and then create an array, that contain all numbers that don't exist in the initial array:

var statues = [1, 5, 2, 7];

function getMissingNumbers(arr) {
  var result = [];
  var map = arr.reduce(function(map, n) { // create a map of existing numbers
    map[n] = true;
    return map
  }, {});

  var max = Math.max.apply(Math, arr); // find the max
  var min = Math.min.apply(Math, arr); // find the min
  
  for(var i = min; i < max; i++) { // run from min to max
    map[i] || result.push(i); // add only numbers that don't exist in the map
  }
  
  return result;
}

var result = getMissingNumbers(statues);

console.log('Missing items: ', result);
console.log('Number of missing items: ', result.length);

Upvotes: 1

Vetrivel
Vetrivel

Reputation: 306

task is to figure out how many numbers is missing between the lowest number and highest number

  1. Sort the numbers : This will give the smallest number and largest number.
  2. Subtract largest number and smallest number : This will give total numbers that could be included that range. Lets say this is N
  3. Subtract Array Length with N : This will give number of missing number in the given array.

Since the question is to count and not to list all the missing numbers, we can take this approach. Following is the code example.

var input = [1,5,2,7];
var sortedInput = input.sort(); // this will work only for single digit array. 
var firstNum = sortedInput[0],
    lastNum = sortedInput[sortedInput.length-1],
    numbersInRange = lastNum - firstNum +2; // +2 to include the numbers that are the range 
    
var missingNumbers = numbersInRange - input.length;

console.log(missingNumbers)

Upvotes: 1

binariedMe
binariedMe

Reputation: 4329

Here is a simple solution you can try :

var a = [1,5,2,7];
a.sort((a, b) => a-b)
 .reduce((acc, element, index) => { 
     if(index) acc = acc + element - a[index-1] - 1; return acc;
 }, 0);

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22534

Find the max and min number and loop through array and check if a number is not part of array.

var arr = [1, 5, 2, 7];
var numberMissing = 0;
for(var i = Math.min.apply(Math, arr) + 1 ; i < Math.max.apply(Math, arr); ++i){
  if(arr.indexOf(i) === -1){
    console.log(i);
    ++numberMissing;
    }
}
console.log("Missing Number : " + numberMissing); 

Upvotes: 1

Related Questions