Basic algorithm scripting concerns

Hello,
I have worked through the basic algorithm scripting section and I feel like my solutions are very sub-optimal. For example, for some challenges, I had to get hints and look at solution code and that code looks very simple and sophisticated.
Meanwhile, take a look at my code from the last challenge. It works but is this good? What can I improve?

function chunkArrayInGroups(arr, size) {
  let result = [];
  let subresult = [];
  let fullArrays = Math.floor(arr.length/size);
  let reminder = arr.length % size;
  let i = 0;
  for (let n = 0; n < fullArrays; n++) {  
      for (i; i < fullArrays * size; i++) {
      subresult.push(arr[i]);
      if (subresult.length == size) {
        result.push(subresult);
        subresult = [];
      }
  }
}
if (reminder != 0) {
  subresult = [];
  for (i; i < arr.length; i++) {
    subresult.push(arr[i]);
  }
  result.push(subresult); 
}

  return result;

}

I have progressed to the final JavaScript challenges and again I am faced with a similar issue. I have passed the Ceasars Cipher challenge, but not sure my code is great:

function rot13(str) {
  let alphabet = {
    "A": 0,
    "B": 1,
    "C": 2,
    "D": 3,
    "E": 4,
    "F": 5,
    "G": 6,
    "H": 7,
    "I": 8,
    "J": 9,
    "K": 10,
    "L": 11,
    "M": 12,
    "N": 13,
    "O": 14,
    "P": 15,
    "Q": 16,
    "R": 17,
    "S": 18,
    "T": 19,
    "U": 20,
    "V": 21,
    "W": 22,
    "X": 23,
    "Y": 24,
    "Z": 25
  }
  let rot13words = str.split(" ");
  let words = [];
  for (let i = 0; i < rot13words.length; i++) { //loop through each coded word
    let word = ""; //word to push later, here to reset for each coded word
    for (let j = 0; j < rot13words[i].length; j++) { // loop through each coded word char
      if (rot13words[i][j].match(/[^\w_]/)) {
        word = word + rot13words[i][j];
        if (j == rot13words[i].length - 1) {
        words.push(word);
      }
        continue;
      }
      let value = alphabet[rot13words[i][j]];
      //console.log("value before decryption: ", value);
      value = value + 13;
      if (value > 25) {
        value = value % 26; // 26 becuase 26 characters in alphabet
      }
      //console.log("value after decryption: ", value);
      //console.log("char after decryption: ", Object.keys(alphabet)[value]);
      //console.log("---------------------------------------");
      word = word + Object.keys(alphabet)[value];
      if (j == rot13words[i].length - 1) {
        words.push(word);
      }

      
    }
  }

  console.log(words);
  return words.join(" ");
}

rot13("SERR CVMMN!");

Is the way to improve studying the solutions? What is the best way to improve in algorithm scripting?

It seems crazy to manually create an object containing every letter of the alphabet.
Fortunately every letter can be traced back to a value which increments as you go though the alphabet (in ASCII: ā€˜A’ = 65, ā€˜B’ = 66, ā€˜C’ = 67, etc.). How could you use these numbers to pass the Ceasars Cipher without hardcoding the value for each letter of the alphabet?

1 Like

Yes, that’s a good idea!

However, I was looking for a more general advice. For a specific problem, I can study better solutions after completing mine, but then I should study each problem individually. I guess that’s also a good way to improve algorithmic thinking, but perhaps there is something else I am missing?

I think just trying to solve as many as you can… you will start to learn more each time you complete something. When you see some code you don’t understand (say ES6’s map function), google/youTube it. There are heaps of good sites for practicing. CodeWars and Exercism are two sites I use to improve my JS.

1 Like

This is my solution…I am very happy to do it without hints!:

Steps:
1- Think on how to get an array of unicode character of str(arr).
2 - Work on arr array depending on the relative position of the character in alphabet unicode array( below 90is yhe unicode of the last alphabetic character Z).When moving the str elements by 13 places we have to garantee to not exceed after Z.else we decrease by 13.
3 - After that we should transform unicode character to character using String.fromCharCode().
4 - Be sure to keep non alphabetic character i.e spaces punctuation…using if condition and replace() method within a map() method on ā€œarrā€.

What do you think about my solution?

function rot13(str) {
var arr = ;

for(var i = 0; i < str.length; i++) {

arr.push(str[i].charCodeAt());

}

for(var j = 0; j < arr.length; j++) {
if(arr[j] + 13 <= 90) {
arr[j] += 13;
}
else arr[j] -= 13;

}
if(/\W$/.test(str)) {
return arr.map(function(item) {
return String.fromCharCode(item);

}).join("").replace(/-/g," ").toString().replace(/.$/,str[str.length - 1]);

}
else return arr.map(function(item) {
return String.fromCharCode(item);

})
.join("").replace(/-/g," ");
}

console.log(rot13(ā€œSERR CVMMN!ā€));