CodeWars "Nice Array" Challenge

Task:

My Code:

function isNice(arr){
  
  for(let i = 0; i < arr.length; i++){
  let n = arr[i]
  if (arr.indexOf(n-1)==-1 && arr.indexOf(n+1)==-1){return false}
  }
  return true
}

My code passes 90%+ of the test cases. I can’t quite think of the edge cases where my code would return an incorrect answer?

I think you are missing to check if arr is empty

2 Likes

Yup, it was just that. Should’ve thought about it more. Thanks a lot!