Skip to content

Instantly share code, notes, and snippets.

@darkcris1
Created January 3, 2021 11:45
Show Gist options
  • Save darkcris1/d14c2c66da035f91cd245160a6f85f36 to your computer and use it in GitHub Desktop.
Save darkcris1/d14c2c66da035f91cd245160a6f85f36 to your computer and use it in GitHub Desktop.
Catching Car Mileage Numbers | Codewars
function isInteresting(number, awesomePhrases = []) {
const str = number.toString(),
isZeros = (num) => /^[1-9](0+)$/.test(num),
isPalindrome = (num) => num.split('').reverse().join('') === num,
isAweP = (num) => awesomePhrases.includes(num),
isSeqInc = (num) =>
RegExp(`${num.length >= 2 ? num : 'a'}`).test('1234567890'),
isSeqDec = (num) =>
RegExp(`${num.length >= 2 ? num : 'a'}`).test('9876543210'),
isClose = (num) => {
const test = 5
const minNum = number - 2
for (let i = 0; i < awesomePhrases.length; i++) {
const phrase = awesomePhrases[i]
if (num >= phrase - 2 && num <= phrase + 2) {
return true
}
}
for (let i = 0; i < test; i++) {
const next = minNum + i
const nextStr = (minNum + i).toString()
if (
isZeros(nextStr) ||
isPalindrome(nextStr) ||
isSeqDec(nextStr) ||
isSeqInc(nextStr) ||
isAweP(next)
) {
return true
}
}
return false
}
if (
number > 100 &&
(isZeros(str) ||
isPalindrome(str) ||
isSeqDec(str) ||
isSeqInc(str) ||
isAweP(number))
) {
return 2
} else if (number > 97 && isClose(number)) {
return 1
}
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment