1
\$\begingroup\$

I have written an isPalindrom-Function in TypeScript.

const isPalindrom = (word: String): boolean => {
    const mid = Math.floor(word.length / 2);

    for (let i = 0; i < mid; i++) {
        const left = word[i].toLocaleLowerCase();
        const right = word[word.length - 1 - i]
            .toLocaleLowerCase();

        if (left != right) {
            return false;
        }
    }

    return true
}

["Anna", "AXnna", 
"Kayak", "kayakx", 
"LEVEL", "LEVExL",
"rotor", "roxtor",
"wow", "wxow",
"mom", "Ymom",
"rEpaPer", "repXaper"]
    .forEach(word => {
        console.log(`Is ${word} a Palindrom? 
=> ${isPalindrom(word)}`);
    });

Is my TypeScript-usage correct?

What could be improved concerning the algorithmic-approach?

I have used the double-equals operator, because the values can't be something else then strings. Is it still necessary to use the triple-equals operator in TypeScript?

\$\endgroup\$
3

1 Answer 1

2
\$\begingroup\$

I have used the double-equals operator, because the values can't be something else then strings. Is it still necessary to use the triple-equals operator in TypeScript?

This is backwards. Necessary? No. Recommended? Yes.

If you know the type is always going to be the same, you should use the triple-equals so as to save the extra checks to see if they are the same. I.e. the triple-equals is simpler than double-equals. Both have to check if the types are the same. With triple-equals, you can stop processing if not. With double-equals, differing types starts a more complicated round of type coercion.

More discussion: You Don't Know JS: Loose vs. Strict Equals

You use the double-equals when the types can be different but you want to coerce them into the same type for equality testing. For example, if 1 == '1'.

All that said, this is not a requirement. Functionally, your code will work with the double-equals. But if you're asking about best practices, then best practice is to use the triple-equals whenever you don't need the type-coercion. It's the double-equals that you should only use when needed. You should default to triple-equals unless it won't work for your use case (because you require type coercion).

Note that even if you do require type coercion, sometimes it is better to do the type coercion explicitly rather than use the loose rules of double-equals. But that won't make a difference here.

Personally, I would prefer to use two index variables rather than calculate one index value from the other.

const normalized = word.toLocaleLowerCase();

for (let left = 0, right = normalized.length - 1; left < right; left++, right--) {
    if (normalized[left] !== normalized[right]) {
        return false;
    }
}

return true;

I find this simpler and easier to follow. Also, it is more easily expandable to cover more complicated normalization. E.g. the removal of punctuation and spaces.

\$\endgroup\$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.