Unexpected loop mistake

Unexpected loop mistake
0.0 0

#1

Tell us what’s happening:

Hi, as always, there are some mathematical imprecisions in the code but what im concerned about is why the loop does not continue workin after processing the first letter.

Your code so far

function rot13(str) {
  var map = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
  str = str.split("");
  for (var i = 0; i < str.length; i++) {
    var e = map.indexOf(str[i]);
    if (e > 13) {
      str[i] = map[e-12];
    } else {
      str[i] = map[e+12];
    }
  }
  str.join();
  return str;
}

rot13("SERR");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:


#2

The loop seems to work fine for me.

You are searching for uppercase letters in an array with lowercase letters, so it won’t find anything and return -1. This results in setting str[i] to map[-1+12] which is the letter l.


#3

i cannot believe it was so simple jajajaja xD