https://www.codewars.com/kata/58f8a3a27a5c28d92e000144
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
Your task is to find the first element of an array that is not consecutive.
By not consecutive we mean not exactly 1 larger than the previous element of the array.
E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that's the first non-consecutive number.
If the whole array is consecutive then return null2.
The array will always have at least 2 elements1 and all elements will be numbers. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!
If you like this Kata, maybe try this one next: https://www.codewars.com/kata/represent-array-of-numbers-as-ranges
1 Can you write a solution that will return null2 for both [] and [ x ] though? (This is an empty array and one with a single number and is not tested for, but you can write your own example test. )
2
Swift, Ruby and Crystal: nil
Haskell: Nothing
Python, Rust, Scala: None
Julia: nothing
Nim: none(int) (See options)
코드
function firstNonConsecutive (arr) {
for(let i = 1; i < arr.length; i++){
if(arr[i] !== arr[i - 1] + 1){
return result = arr[i];
}
}
return null;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Regex count lowercase letters (8 kyu) / JavaScript (0) | 2023.04.27 |
---|---|
[Codewars] L1: Bartender, drinks! (8 kyu) / JavaScript (0) | 2023.04.26 |
[Codewars] Simple validation of a username with regex (8 kyu) / JavaScript (0) | 2023.04.25 |
[백준] 26529: Bunnies / Node.js (JavaScript) (0) | 2023.04.25 |
[Codewars] Get number from string (8 kyu) / JavaScript (0) | 2023.04.25 |
댓글