Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 863 Bytes

File metadata and controls

42 lines (34 loc) · 863 Bytes

is Palindrome

concept

// return true/false
function isPalindrone (string) {
    ...
}

判斷字串是否為 Palindrome, Palindrome 例子:

  • race car
  • Madam, I'm Adam

example code

const isPalindrome = string => {
    // 去除所有非英文字母
    const pureCharacters = (str => {
        const validCharacters = `abcdefghijklmnopqrstuvwxyz`.split(``);
        return str
            .toLowerCase()
            .split(``)
            .filter(char => validCharacters.includes(char));
    })(string);

    return (
        pureCharacters.join(``) ===
        pureCharacters.reverse().join(``)
    );
};

console.log('--------');
console.log(isPalindrome(`Madam, I'm Adam.`)); // true
console.log(isPalindrome(`race car`)); // true 
console.log(isPalindrome(`I'm Adam.`)); // false
console.log('--------');