Skip to content

Instantly share code, notes, and snippets.

@borispov
Created October 6, 2018 22:03
Show Gist options
  • Save borispov/33f0cc2cee5ef5bff113f32262b49552 to your computer and use it in GitHub Desktop.
Save borispov/33f0cc2cee5ef5bff113f32262b49552 to your computer and use it in GitHub Desktop.
Simple solutions for isPalindrome, snake_case coverter and fibonacci
// check if word is palindrome
// can enter CAPS, nums, symbols etc..
function isPalindrome(string){
return string.replace(/[^a-z]/gi, '').toLowerCase().split('').reverse().join('') === string.replace(/[^a-z]/gi, '').toLowerCase()
}
// n is index in the fibonacci sequence.
// function returns n's number in the sequence.
function fibo(n) {
if (n < 0) return 'OOPS!'
if (n <= 1) return n
return fibo(n - 1) + fibo(n - 2)
}
// convert strings to snakeCase: hello_world
// I have found that it's easier done with Regular Expressions than with convert to an Array and 'algorithimize' it.
function makeSnake(string) {
return string.replace(/[^a-z]/gi, ' ').replace(/ {1,}/g," ").trimRight().split(' ').join('_').toLowerCase()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment