I wrote a simple for loop, to understand charCodeAt() and fromCharCode() better and to solve the odin project the Caesar cipher exercise where you have to write function to encode a string.
It stops at the first iteration and I don“t know why. When I console log str.charCodeAt(i) it loops, but when I add the rest of the function it stops.
function uniCode(str, num) {
for(let i = 0; i < str.length;i++) {
//console.log(str.charCodeAt(i) + num);
let charCode = str.charCodeAt(i) + num;
let newStr = String.fromCharCode(charCode);
return newStr;
}
}
uniCode("Help!", 3);
'K'
Glad for any help!
Thanks!
with your code you loop through all characters of a given string in parameter
but you have
return newStr;in the loop it will stop the loop iteration