Skip to content

Instantly share code, notes, and snippets.

@CarlMungazi
Last active November 24, 2016 20:05
Show Gist options
  • Save CarlMungazi/44669e1a9ea16fae23b878e8b08e0ff6 to your computer and use it in GitHub Desktop.
Save CarlMungazi/44669e1a9ea16fae23b878e8b08e0ff6 to your computer and use it in GitHub Desktop.
FCC Bonfire - Check for Palindromes

Task: Write a function which returns true if the given string is a palindrome. Otherwise, return false.

Pseudocode:

  • Get the string value as a parameter
    • If the user has entered a string
      • Remove non-alphanumeric characters and make the string lowercase
      • Reverse the string
      • If the string is a palindrome
        • return the boolean value true
      • If the value is not a palindrome
        • return the boolean value false
    • If the given value is not a string
      • Ask the user to enter a string

Actual Code:

function palindrome(str) {
    if (typeof str === 'string') {
      // remove non-alphanumeric characters and make str lowercase
      var removeChar = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();
      // reverse str variable
      var checkPalindrome = removeChar.split('').reverse().join('');

      // check if str is a palindrome
      if (removeChar === checkPalindrome ) {
        return true;
      } else {
        return false;
      }
    } else {
        return "Please enter a string";
    }
  }
palindrome("car"); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment