Greetings, I hope you can help me, I can't find my error, what happens is that I am making a function that solves the following problem: Make the function ArrayChallenge( arr ) read the array of integers stored in arr that will have the following format: [K , r1, r2, r3, ...] where K represents the number of desks in a classroom and the rest of the integers in the array will be ordered and represent the desks that are already occupied. All desks will be arranged in 2 columns, where desk #1 is top left, desk #2 is top right, desk #3 is below desk #1, desk #4 is below #2, etc. should return the number of ways 2 students can sit next to each other.
For example: if arr [12, 2, 6, 7, 11], there are a total of 6 ways to seat 2 new students next to each other. The combinations are: [1, 3], [3, 4], [3, 5], [8, 10], [9, 10], [10, 12]. So for this input, your program should return 6 .
function ArrayChallenge(arr) {
let highest = arr[0];
let lowest = arr[0];
let numberBetween = [];
for(let i = 0;i<arr.lenght;i++){
if(arr[i]>highest){
highest=arr[i];
}
else {
if(arr[i]<lowest){
lowest=arr[i];
}
}
}
console.log('highest', highest);
console.log('lowest', lowest);
for(let j = lowest;j<=highest;j++){
if(arr.indexOf(j)==-1){
numberBetween.push(j);
}
}
console.log('numberBetween', numberBetween);
console.log('arr', arr);
return numberBetween.length;
}
this is the function i try to solve but i get 0; Thanks for the help.