Caesars Cipher --- interesting things in for loop logic

Very interesting when I do Caesars Cipher task and I found Code Segment 1 succeed but 2 failed.

Any thoughts?

#################Code Segment 1 ######################
for(i=0;i<str.length;i++){
if(str.charCodeAt(i)>77 && str.charCodeAt(i)<91){
resultStr=resultStr+String.fromCharCode(13-90+64+str.charCodeAt(i));
}else if(str.charCodeAt(i)>64 && str.charCodeAt(i)<78){
resultStr=resultStr+String.fromCharCode(str.charCodeAt(i)+13);
}else{
resultStr=resultStr+String.fromCharCode(str.charCodeAt(i));
}
}
return resultStr;

#################Code Segment 2 ######################

for(i=0;i<str.legnth;i++){
if(str.charCodeAt(i)>64 && str.charCodeAt(i)<78){
resultStr=resultStr+String.fromCharCode(str.charCodeAt(i)+13);
} else if(str.charCodeAt(i)>77 && str.charCodeAt(i)<91){
resultStr=resultStr+String.fromCharCode(13-90+str.charCodeAt(i)+64);
}
else{
resultStr=resultStr+String.fromCharCode(str.charCodeAt(i));
}
}

return resultStr;