Caeser's Cipher - array of undefined

So I’m doing Caeser’s Cipher from the JavaScript projects.
At first I thought I had it with this:

function rot13(str) { // LBH QVQ VG!
  let arr = str.split('');
  let newArr =[];
  for (let i = 0; i < str.length; i++) {
    let newArr = arr.map((item) => item.charCodeAt(i) - 13);
    return newArr.map((item) => String.fromCharCode(item)).join('')
    
  }       
}

but then I realised that only worked with half the letters, so I tried this (probably gobbledegook):

function rot13(str) { // LBH QVQ VG!
  let arr = str.split('');
  let newArr =[];
  for (let i = 0; i < str.length; i++) {
    let newArr = arr.map((item) => {
      if (item.charCodeAt(i) >= 78) {
        item = item.charCodeAt(i) - 13;
      }
      else {
        item = item.charCodeAt(i) + 13;
      }
    })
    return newArr;
  }       
}

I’m trying to do two different things to the items depending on where they come in the alphabet but I can’t find the syntax to do this. This comes back as an array of undefineds.

Pls help! Thanks

First, take a deep breath. Then realize that the point of the Array functions map, reduce, and filter is to iterate over the array FOR YOU, so you don’t have to write a for loop to do it.

In Array.prototype.map you are passing a callback (contents of the callback replaced with an ellipsis:

  let newArr = arr.map((item) => { ... });

The code block {...} needs to RETURN a value for each item of the array, like this MAP FUNCTION that takes the original array at left as the DOMAIN and MAPS ONTO the RANGE at right:

So, for each element, the callback function (the big black arrow in the middle) needs to take in an array element (item), and return a value (item.name)

Walk through a sample input to your code with a pen and paper and document each pass through the loop. What is i? What is item? What is item[i]?

Thanks guys, I’m probably doing things above my level I just think it could be a good way to learn.
I used the for loop because I had no idea how to target the character code for all the letters, but I think if I put i = item.charCodeAt(0) with the map function that might help me get rid of that weird part.

Your level is what you learn how to do, so your approach is fine. I didn’t get the hang of Array iteration functions until I spent a lot of time failing at them, with MDN’s reference page (linked previously) open. The Syntax section is the most useful section, because it tells you exactly what you have to put into every function, and what you can expect to get out of it.

Your gut feeling about mapping from each item in the array of characters and transforming to ASCII then working on the numeric representation is correct.

HINT: You need EITHER a for loop to iterate over the array for(let i=0; i<arr.length; i++){resultArr[i] = what you want to turn arr[i] into}, or a map function: arr.map(item => {return what you want to turn item into});

Hi,
I did some more reading around map (although it still confuses me!)
I hadn’t realised that you could alter the array by using a return within a function in map and it would keep the array in the same order! That will be helpful for other challenges.
Thanks!

1 Like

Yeah, map/filter/reduce is a very useful toolset. Just to clarify, what you do mean by “alter the array”

result = arr.map( callbackFunction );

assigns to result the new array created by applying callbackFunction to every element of arr. Yes, the indices are in the same order, but you’re not altering arr, you’re creating result.

I just want to make sure we’re on the same page on this.

sorry yes I’m not very precise with the programming language yet…

I just mean that putting return inside the if/else statements within the map function allowed me to create an array of the character codes of all the letters in the array which I could then - or + 13 and convert it back into letters, which is what I was trying to do with the
item = item.charCodeAt(i) + 13 parts!

I’d post what I’d done to pass the tests but I didn’t manage to save it before I submitted and it’s in one of about a thousand repl.its I have going.

You can download the JSON file of your solution after submitting. Underneath the default button in Green, there’s a button in white that says “Download My Solution”

Can I do that even after I’ve clicked onto the next challenge? I clicked that out of habit.

Unfortunately, no. But, that’s why FCC requires two actions to pass on and lose the code:
Mouse: “Run the Tests” “Submit and go to the next challenge”
Keyboard: Ctrl+Enter x 2.

After you get to the screen I captured above, you can press Escape to go back and either cut/paste the solution, or play around some more. Using the mouse, you can press the “X” in the upper-right corner of the modal window.

Your solution is probably still in your browser’s cache, if you go back to the challenge.