I'm checking if the given string is palindrom or not, but the execution time is too long.
My code is below:
function checkPalindrome(inputString) {
for (let i = 0; i < (inputString.length - 1) / 2; i++) {
const a = inputString[i];
const b = inputString.split("").reverse().join("")[i];
if (a !== b) {
return false;
}
continue;
}
return true;
}
const palindrome = checkPalindrome("abba");
console.log(palindrome);
How can I optimize the code in order to reduce it's execution time?
Why you are iterating each letter, When you can reverse it using array methods?
function checkPalindrome(inputString) {
return inputString === inputString.split("").reverse().join("");
}
let palindrome = checkPalindrome("abba");
console.log(palindrome);
palindrome = checkPalindrome("random");
console.log(palindrome);
palindrome = checkPalindrome("abba isi abba");
console.log(palindrome);