2018-Apr-19, Thursday

Yrneavat gb Pbqr

2018-Apr-19, Thursday 14:57
dorchadas: (Broken Dream)
I just realized that I still have a Megatokyo icon in there. That demonstrates how long I've been posting on DW/LJ.

Post-Skyward Sword, I've returned to working more on the freeCodeCamp curriculum. Just today I finished the basic algorithms section, after many hours of banging my head against the wall. It's all simple stuff like adding multiple numbers, changing every element of an array, sorting a list numerically or alphabetically, splitting an array into piece (that one took a while), and so on.
Here's the code for my ROT13 encryption solution with comments removed:
function rot13(str) {
var answerStr = "";
for (var i = 0; i < str.length; i++) {
if (/[A-Z]/.test(str.charAt(i))) {
answerStr += String.fromCharCode((str.charCodeAt(i) % 26) + 65);
}
else {
answerStr += str.charAt(i);
}
}
return answerStr;
}

Here's how it works:
  1. takes in an uppercase string. It won't work for lowercase, for reasons I'll get to.
  2. Checks the string character by character. If it's not an alphabetic character--if it's a number, a space, punctuation, etc.--it just adds it to the answer.
  3. If it is an alphabetic character, it gets its HTML character code. For uppercase, this is between 65 and 90.
  4. It then modulos the answer by 26, which leaves a result between 0 and 25. This also converts it to the ROT13 equivalent. The character code for A is 65, but 65 % 26 is 13.
  5. Add 65 to the result of the previous equation, again giving each character a result of between 65 and 90. This is also why only uppercase works, since lowercase has character codes in a different range.
  6. Convert those character codes to the alphabet equivalent and add it to the answer.
  7. Repeat until the input is all processed.
I used to have a second If statement, where I'd add 13 to the character code unless it went over 90, in which case I'd subtract 13, but this implementation is cleaner.

And there are other ways to do it. When I finished I checked the thread for other solutions and saw a lot of people converted the string into an array and used the .map function to process each character and then .join to make an answer at the end. Some people wrote out an index, like var abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", and then used that to make the substitutions. One guy wrote a long multidimensinal array (var data = [["A", "N"], ["B", "O"], etc) and just swapped characters, which does technically work. One person wrote a 27-case switch statement. Emoji Smiling sweatdrop

Maybe that's what makes me interested in programming. In some ways it's like translation, taking a specific meaning and rendering into an understandable text, it's just that the text has to be understandable to computers. And much like learning a new human language, it requires creating a ton of garbage before anything worthwhile comes out. Emoji Oh dear

Next up is JSON and APIs, and then more dev projects. A random quote machine, the local weather (though the example for that project thinks I'm in Shuzenji in Shizuoka...), and a wikipedia viewer. I forsee a lot more banging my head against the wall.