This code works. When I saw the solutions, the basic solution had the string converted to an array.
I would appreciate if someone could please explain why this wouldn’t be a good approach, and what are the shortcomings here. My code is :
function rot13(str) { // LBH QVQ VG!
let initial=0;
let shifted = 0;
let i = 0;
let str2='';
for ( i= 0; i<str.length;i++){
initial = str.charCodeAt(i);
console.log(initial);
if (initial <65 || initial>90){
shifted = initial;
}
else if (initial >= 78) {
shifted = 65 +(initial-78);
}
else{
shifted = initial +13;
}
str2+= String.fromCharCode(shifted);
}
return str2;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
