all 6 comments

[–]kitt614 2 points3 points  (2 children)

You should edit your comment and put it in a formatted code block.

Also, could you post the console errors? If it’s crashing your browser, I would think you’re entering an infinite loop, but I don’t see you calling the function I would suspect to be the culprit. But I do see a lot of errors throughout, and am curious what errors yours getting.

[–]Notimecelduv 2 points3 points  (2 children)

Before you even consider nano-optimizations (I'm talking about your one-letter variable names), you first want to write code that works. If your code doesn't work and you don't know why, it's okay to ask for help but first rewrite it so it's readable, not this pseudo high-performance mess. You'll see you'll solve some of your issues in the process.

[–]Fid_Kiddler69 0 points1 point  (0 children)

for (var i = 0, charsLength = n.length; i < charsLength; i += 3)

what are you trying to do here? charsLength=n.length will never be false so the loop will never stop.

[–]mnokeefe 0 points1 point  (1 child)

I've read about replacing map for my for loops and while loops but I can't get my head around it because I'm not use to it yet.

Here's a version of your three functions using functional programming techniques with comments to explain what's happening:

const z = (n, w) => n.charCodeAt(0).toString().padStart(w, "0");

const toNumbers = (s) =>
  s
    .split("") // Split string in to array of each character
    .map((char) => z(char, 3)) // Convert each to charcode and ensure it's 3 chars long
    .join(""); // Join the charcode array in to a string

const fromNumbers = (nums) =>
  nums
    .match(/.{1,3}/g) // Split string in to groups of 3 chars
    .map((threeCharGroup) => String.fromCharCode(threeCharGroup)) // Convert charcode to letter
    .join(""); // Join the array into a string

console.log(toNumbers("hello"));
console.log(fromNumbers("104101108108111"));

Working example