JavaScript Get quantity of missed elements from an array

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and system doesn't accept the code. I tried some methods, but unfortunately they are useless.

function arr(x){
 let y = [];
 for (let i = x[0]; i <= x[x.length-1]; i++){
    y.push(i);
 }
 return y.length-x.length;
}
let m = arr([1,2,3,4,5,6]);
console.log(m);

Or...

function arr(x){
    let y = [];
    for (let i = 0; i < x.length; i++){
        for (let j = 0; j < i; j++){
            if (x[i] == x[j]){
                x.splice(i,1);
                i--;
            }
        }
    }
    console.log(x);
    for (let i = x[0]; i <= x[x.length-1]; i++){
        y.push(i);
    }
    console.log(y);
    return y.length-x.length;
}
let l = arr([1,3,2,4,9]);
console.log(l);

I also tried to sort array, but there are no changes

Answer:1

You can do that in following steps:

  • Remove the duplicate elements from array.
  • Get the range of array by subtracting first value from last.
  • Subtract the range from the length of array.

Note: I am considering array is sorted.

function missed(arr){
  arr = [...new Set(arr)];
  return arr[arr.length - 1] - arr[0] - arr.length + 1
}

console.log(missed([1,3,6]))
Answer:2

I don't know if optimization is a must, bust the easiest non-ES6 (for all browsers support) way I can think of is to sort the array and iterate through it logging the numbers that you expect but are missing. Something like this:

var arr = [4,1,6,8];
arr.sort();
var miss = [];
var expect = arr[0]; // The starting number is the first element in the array I guess. Change to 1 if you want it to be 1

// Loop through the sorted array
for (var i = 0; i < arr.length; i++) {
    while(arr[i] > expect++) { // keep looping while the current number is not the one expected, and increment the expected by one
        miss.push(expect-1); // If missed, record the missed number (which is the actual expected minus 1, as we already incremented it)
    }
}

console.log(miss);
Answer:3

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

  1. file with node
  2. file upload with node js
  3. download file with node
  4. run file with node
  5. read file with node
  6. create file with node
  7. download file with node js
  8. read file with node js
  9. create file with node js
  10. open file with node.js
  11. delete file with node js
  12. write file with node js
  13. write file with node
  14. delete file with node
  15. upload file with node
  16. file upload with node and react
  17. open file with node
  18. file server with node.js
  19. node dockerfile
  20. send file with node js

As mentioned in the interface section of 'Programming javascript applications' you can implement interfaces with stampit. When reducing the unnecessary stuff from the example, I end up with something ...

As mentioned in the interface section of 'Programming javascript applications' you can implement interfaces with stampit. When reducing the unnecessary stuff from the example, I end up with something ...

I'm debugging someone's code and wondering what this regex does? Code: <script> var t = document.getElementById("filterVal").value; var s = filterVal.replace(/"([^"]+(?="))"/g, '$1') </...

I'm debugging someone's code and wondering what this regex does? Code: <script> var t = document.getElementById("filterVal").value; var s = filterVal.replace(/"([^"]+(?="))"/g, '$1') </...

  1. what does this emoji mean
  2. what does this mean �� ��
  3. what does this symbol mean
  4. what does this emoji mean ��
  5. what does this sign mean
  6. what does this mean in english
  7. what does this word mean
  8. what does this emoji mean ��
  9. what does this emoji mean ��
  10. what does this emoji mean ��
  11. what does this emoji mean ��
  12. what does this mean in texting
  13. what does this national emergency mean
  14. what does this emoji mean ��
  15. what does this symbol mean in math
  16. what does this quote mean
  17. what does this mean in spanish
  18. what does this mean in math
  19. what does this mean 3
  20. what does this emoji mean ��

How do I use a dependency injected field in another field within the ember controller in 2.x Ember? For instance, I have export default Ember.Controller.extend({ session: Ember.inject.service(...

How do I use a dependency injected field in another field within the ember controller in 2.x Ember? For instance, I have export default Ember.Controller.extend({ session: Ember.inject.service(...

  1. using dependency injection c#
  2. using dependency injection in unit tests c#
  3. using dependency injection in asp.net core
  4. using dependency injection in console application c#
  5. using dependency injection in a class library
  6. using dependency injection in .net core
  7. using dependency injection in a class library c#
  8. using dependency injection in mvc 5
  9. using dependency injection in web api
  10. using dependency injection in unit tests
  11. using dependency injection in java
  12. using dependency injection
  13. using dependency injection with entity framework
  14. using dependency injection with startup.cs in asp.net core
  15. using dependency injection in windows service
  16. using dependency injection with annotations
  17. using dependency injection in asp net webforms
  18. using dependency injection swift
  19. using dependency injection with framework
  20. using dependency injection in laravel