DEV Community

Discussion on: Top 26 JavaScript Interview Questions I Wish I Knew

Collapse
 
zcs19871221 profile image
zcs1

hi,it is a good article.but it has a problem:
Q20:
for your function ,it seemed wrong:
isIsomorphic('sad', 'egg') !== isIsomorphic('egg', 'sad')
my function:

function isIsomorphic(stra, strb) {
if (typeof stra === 'string' && typeof strb === 'string') {
    const len = stra.length;
    if (len !== strb.length) {
        return false;
    }
    const mapa = {};
    const mapb = {};
    function countApperTimes(map, pro) {
        map[pro] = map[pro] ? map[pro] + 1 : 1;
    }
    for (let i = 0; i < len; i++) {
        const a = stra[i];
        const b = strb[i];
        countApperTimes(mapa, a);
        countApperTimes(mapb, b);
        if (mapa[a] !== mapb[b]) {
            return false;
        }
    }
    return true;
}
return false;
}