My code runs correctly in the console but produces incorrect results in Hackerank Issue: Determine the count of all substring palindromes within a given string
function isPalindrome(str) {
var len = str.length;
var mid = Math.floor(len / 2);
for (var i = 0; i < mid; i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
// This function had to be implemented because I encountered an error using the built-in method.
// Original line that caused errors:
// str == str.split('').reverse().join('');
return true;
}
function scatterPalindrome(str) {
var result = [],
c = 0;
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length + 1; j++) {
result.push(str.slice(i, j));
}
}
for (let i = 0; i < result.length; i++) {
let k = result[i];
if (isPalindrome(k))
c++;
}
return c; // The output was consistently returning 1
}
console.log(scatterPalindrome("abc"));
input: "abc"
expected output: 3
actual output:1