4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

JS Interview Prep: Palindrome

Screenshot of Google Chrome (11-23-17, 9-09-30 PM).png

Question

Given a string, have the function return true or false if palindrome

Examples:
palindrome("abba") === true
palindrome("abcdefg") === false

Answer

Solution 1

function palindrome(str) {
  return str.split('').every((c, i) => {
    return c === str[str.length - i -1];
  });
}

Solution 2

function palindrome(str) {
  const reversed = str
                   .split('')
                   .reverse()
                   .join('');
  
return reversed === str;
}

References

Every Function
1 upvotes $0.00

Replies (0)

Conversation

No replies yet

Replies will appear here as people join the conversation.

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: