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
You can do that in following steps:
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]))
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);
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. ...
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') </...
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(...