JSFiddle - React, Tailwind, and code Playground

by Alexey

JavaScript

function makeArrayConsecutive2(statues) {
var st = statues;
var statues2 = st.sort(function(a, b) {
  return a - b;
})
let sum = [];
for (var i = 0; i < statues2.length; i++) {
  let sum2 = statues2[i + 1] - statues2[i];
  if (sum2) {
    sum.push(sum2 - 1);
  }
}
var fin = sum.reduce(function(a,b) {
  return a+b},0
                    )
if(st.length===1) {
  console.log(0);
} else {console.log(fin)}

}
makeArrayConsecutive2([1])