4 ms·
Here's an example that made sense to me elegant (readable / compact) function isPalindrome(str) { return str == str.split('').reverse().join(''); } effic
by sebringj 8y ago
Here's an example that made sense to me
elegant (readable / compact)
function isPalindrome(str) {
return str == str.split('').reverse().join('');
}
efficient (~50x faster than above)
function isPalindrome(str) {
var len = Math.floor(str.length / 2);
for (var i = 0; i < len; i++)
if (str[i] !== str[str.length - i - 1])
return false;
return true;
}
I would probably say elegance and efficiency are not aligned rather than at odds IMO because they may overlap or may not, they don't have the same goals.