JSFiddle - React, Tailwind, and code Playground
by SriSri M
JavaScript
input = 'asdasdaa'
charAndCount = [];
sortedArray = [];
temp = [];
if(input !== ''){
input = input.split('');
for(var i =0; i< input.length ; i++){
var count = 0;
for(var j =0; j< input.length ; j++){
if(input[i] == input[j])
count +=1;
}
if(temp.indexOf(input[i]) == -1){
charAndCount.push({
'character': input[i],
'charLength': count
})
}
temp.push(input[i])
}
totalLength = input.length;
if(totalLength % 2 == 0){
sortedArray = charAndCount.filter(e => e.charLength % 2 == 0)
if(sortedArray.length == charAndCount.length){
alert ('String makes a palindrom')
}
}
else {
if(totalLength % 2 == 1){
sortedEvenArray = charAndCount.filter(e => e.charLength % 2 == 0);
sortedOddArray = charAndCount.filter(e => e.charLength % 2 == 1)
if(sortedEvenArray % 2 == 0 && sortedOddArray %2 ==1){
if((sortedEvenArray.length + sortedOddArray.length) == charAndCount.length)
alert ('String makes a palindrom')
} else{
alert ('String dose not make a palindrom')
}
}
}
}