Hello!
Not the most refined code but I think I’m almost there. My goal is to solve then refine where I can. Anyways, I’m getting weird characters added to my code somehow.
["F", "R", "E", "E", " ", "C", "O", "Da", "E", " ", "C↵", "A", "M", "P"]
The above array was copied/pasted directly from chrome js console. It is the result of console.log(result);
Looking at the above array, three odd characters are coming up in my code :
1)“Da” - ok, when looking at this while I type this topic preview and in my js console there is no “Da” it looks like “D”. When I submit this topic it comes up “Da”. ??
2)whitespace - the last whitespace between “E” & “C↵” in my chrome js console actually looks like a string with whitespace but two characters long such as “blank blank” and not just “blank”. It is not showing in the preview as I type this topic.
3)“C↵” - where the heck did this come from??
I would like it to look like :
["F", "R", "E", "E", " ", "C", "O", "D", "E", " ", "C", "A", "M", "P"]
Does anyone know why I get a whitespace character with twice the whitespace, the very odd "C↵" and why “D” comes out “Da” ? Bizarre!
function rot13(str) {
//string to array :
var arr = str.split('');
console.log(arr);// ["S", "E", "R", "R", " ", "P", "B", "Q", "R", " ", "P", "N", "Z", "C"]
//empty array will hold unicode
var unicode = [];
//empty array that will hold shifted unicode
var shifted = [];
//loop through arr and change each letter to unicode :
for(var i = 0; i < arr.length; i++) {
var toUnicode = arr[i].charCodeAt(arr[i]);
unicode.push(toUnicode);
}
console.log(unicode);//[83, 69, 82, 82, 32, 80, 66, 81, 82, 32, 80, 78, 90, 67]
//we need to loop through array unicode and check whether each item needs
//to be shifted by +13 or by -13
//we also need a place to put the new values
for(var j = 0; j < unicode.length; j++) {
if(unicode[j] == 32) {
shifted.push(unicode[j]);
}
else if (unicode[j] >= 65 && unicode[j] <= 77 ) {
shifted.push(unicode[j] + 13);
} else {
shifted.push(unicode[j] - 13);
}
}
console.log(shifted);//[70, 82, 69, 69, 32, 67, 79, 68, 69, 32, 67, 65, 77, 80]
var result = shifted.map(String.fromCharCode);
console.log(result);//["F", "R", "E", "E", " ", "C", "O", "Da", "E", " ", "C↵", "A", "M", "P"]
}
rot13("SERR PBQR PNZC");