Code not working only in FCC?

I’m currently doing the Caesar encryption task & i’ve created a solution for it that isn’t working in the FCC compiler but works everywhere else for me?

My Code
https://playcode.io/1001574

I’m not passing the assignment and getting the error “ReferenceError: i is not defined”

I get there’s more elegant ways to solve this, but this is what I came up with

Code:

function rot13(str) {
  const alphaValue = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5,
    f: 6,
    g: 7,
    h: 8,
    i: 9,
    j: 10,
    k: 11,
    l: 12,
    m: 13,
    n: 14,
    o: 15,
    p: 16,
    q: 17,
    r: 18,
    s: 19,
    t: 20,
    u: 21,
    v: 22,
    w: 23,
    x: 24,
    y: 25,
    z: 26,
  };

  const numValue = {
    1: 'a',
    2: 'b',
    3: 'c',
    4: 'd',
    5: 'e',
    6: 'f',
    7: 'g',
    8: 'h',
    9: 'i',
    10: 'j',
    11: 'k',
    12: 'l',
    13: 'm',
    14: 'n',
    15: 'o',
    16: 'p',
    17: 'q',
    18: 'r',
    19: 's',
    20: 't',
    21: 'u',
    22: 'v',
    23: 'w',
    24: 'x',
    25: 'y',
    26: 'z',
    27: 'a',
    28: 'b',
    29: 'c',
    30: 'd',
    31: 'e',
    32: 'f',
    33: 'g',
    34: 'h',
    35: 'i',
    36: 'j',
    37: 'k',
    38: 'l',
    39: 'm',
    40: 'n',
    41: 'o',
    42: 'p',
    43: 'q',
    44: 'r',
    45: 's',
    46: 't',
    47: 'u',
    48: 'v',
    49: 'w',
    50: 'x',
    51: 'y',
    52: 'z',
  };

  console.log('input: ' + str);
  let input = str.toLowerCase();

  let accumulator = '';
  let allowedChars = [' ', '!'];

  for (i = 0; i < input.length; i++) {
    if (
      input[i] !== ' ' &&
      input[i] !== '!' &&
      input[i] !== '.' &&
      input[i] !== '?' &&
      input[i] !== '/' &&
      input[i] !== '(' &&
      input[i] !== ')'
    ) {
      keyLookup = alphaValue[input[i]];
      newKey = keyLookup + 13;
      accumulator += numValue[newKey];
    } else accumulator += input[i];
  }

  console.log('output: ' + accumulator.toUpperCase());
  return accumulator.toUpperCase();
}

rot13('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.');


here is your variable i

something is missing…

(want another hint?)

add a let

1 Like

I cant believe i missed this haha

to add to this, i’ve spent the past 2 hours trying to do a loop of an array inside that loop to add allowedChars [‘!’, ’ ', ‘?’ etc…]

I wonder if thats why it wasnt working!

I actually missed loads of let’s out, I wonder why it even worked in the first place?

Is it because undefined resolves as false, 0?

Modify your function so it looks like this:

Now it will stop working everywhere else as well.

JS allows setting variables without a keyword in front of them; they automatically become global. One of the things strict mode does is prevent that undesirable behaviour, but it has to be explicitly turned on as above, which is why your code works elsewhere.

FCC sets strict mode on the code you write automatically (IIRC apart from a few earlier challenges which try to explain the problem with global variables), so your code errors.

1 Like

Thanks a lot, I wondered what strict was but could never conceptualise it until now.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.