It may well be an error @ Hackerrank. If I'm not mistaken the nodejs-code expects you to provide console input. Or you may have accidentally changed something in the surrounding code.
Concerning your code: writing ES20xx, it's good practice to terminate lines with a semicolon (;
), because not doing so may result in nasty bugs.
let palindrome = s === s.split('').reverse().join('')
You don't need this variable. It could've been:
if(s !== s.split('').reverse().join('')) {
Furthermore, if you wanted to declare a variable, it could've been a const
here (you are not modifying it afterwards).
Just for fun, here's an alternative approach, using substrings from the original given string:
"hannach,ava,reopaper,annana,ewve,blob,otto,michael,racecaar,wasitacatiwsaw" .split(",") .forEach(name => console.log(`[${name}] => ${palindromeIndex(name)}`)); function palindromeIndex(s) { if (`${[...s]}` === `${[...s].reverse()}`) { return "is palindrome"; } let i = 0; while(i < s.length) { const sx = `${i < 1 ? s.substr(1, 0) : s.substr(0, i)}${s.substr(i + 1)}`; const rsx = `${[...sx].reverse().join("")}`; if (sx === rsx) { return `removing '${s[i]}' (@ position ${i}): '${sx}'`; }; i += 1; } return -1; }
.as-console-wrapper { top: 0; max-height: 100% !important; }