well this a caesars cipher project and its saying " Cannot read properties of undefined (reading ‘charCodeAt’)"
function rot13(str) {
var solved = "";
for( var i = 0; i <= str.length; i++) {
var asciiNum = str[i].charCodeAt();
if (asciiNum >= 65 && asciiNum <= 77) {
solved += String.fromCharCode(asciiNum + 13)
} else if (asciiNum >= 78 && asciiNum <= 90) {
solved += String.fromCharCode(asciiNum - 13)
} else {
solved += str[i];
}
}
return solved;
}
rot13("SERR PBQR PNZC");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher
Link to the challenge:
You could check this out: str.length = 14
I played with your code a tad. two things you’ll need to change:
according to MDN, the format should be str.charCodeAt(i);
secondly, this part needs adjusting:
redietkebedewor:
i <= str.length
By the way, props for using the ascii methods. I didn’t think to check this when I passed this test
oh thanks…I researched methods to solve the problem and i stumbled on this way of solving it
did you get it to work? When I tried out your code, you were very close.
ya but when it prints to console it prints the wanted result with undefined at the end of the sentence
redietkebedewor:
i <= str.length
that’s because of this part
ok so from the for loop but what should it be
if i were representing an array, remember that arrays are zero-indexed. However, str.length is not.
what that means is if a string (lets say the word “string”) is 6 characters long, the index of the last letter would be 5 (it starts counting from zero.)
If you were to check the character at index 6, even though it’s 6 letters long, you get undefined as the answer because you’re checking an index that doesn’t exist.
TL;DR : arrays and .length are not exactly equal
ohhh thanks know i understand
system
Closed
February 23, 2024, 12:51am
11
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.