Edabit

Given a word, create a function which returns whether or not it's possible to create a palindrome by rearranging the letters in the word.

Examples

 isPalindromePossible("rearcac") ➞ true // You can make "racecar"  isPalindromePossible("suhbeusheff") ➞ true // You can make "sfuehbheufs" (not a real word but still a palindrome)  isPalindromePossible("palindrome") ➞ false // It's impossible 

Notes

  • Trivially, words which are already palindromes return true.
  • Words are given in all lowercase.

Solution:

function isPalindromePossible(str) {   let chars = {};   for (let char of [...str]) {     if (chars[char]) {       chars[char] += 1;     } else {       chars[char] = 1;     }   }   if (str.length % 2 == 0) {     let arr = Object.values(chars).filter((val) => val % 2 == 1);     //console.log("even", chars);     if (arr.length === 0) {       return true;     } else {       return false;     }   } else {     //console.log("odd", chars);     let arr = Object.values(chars).filter((val) => val % 2 == 1);     if (arr.length === 1) {       return true;     } else {       return false;     }   } } 

This free site is ad-supported. Learn more