Rot13 js project not working

My code:

// A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.

// Write a function which takes a ROT13 encoded string as input and returns a decoded string.

// All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.

// rot13("SERR PBQR PNZC") should decode to FREE CODE CAMP
// rot13("SERR CVMMN!") should decode to FREE PIZZA!
// rot13("SERR YBIR?") should decode to FREE LOVE?
// rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") should decode to THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

let numOfLetters = 26;
let offSet = 13;

let alphabet = [
  "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",
];

function rot13(aString) {
  // string to lowercase.
  let lowercaseString = aString.toLowerCase();
  // get string into an arr
  let arr = [];
  for (i = 0; i < lowercaseString.length; i++) {
    arr.push(lowercaseString[i]);
  }
  //   console.log(arr);
  // decode each element on the array if is a letter.
  const newArr = arr.map((value, index, array) => {
    if (alphabet.indexOf(value) !== -1) {
      // decode character
      const char = decodeOneChar(value, offSet);
      return char;
    } else {
      return value;
    }
  });
  // join the array
  return newArr.join("").toUpperCase();
}

function decodeOneChar(encodedChar, offSet) {
  // given a encoded letter and the offset returns the decoded character
  const encodedCharPosition = alphabet.indexOf(encodedChar);
  let decodedCharPosition;
  if (encodedCharPosition - offSet >= 0) {
    decodedCharPosition = encodedCharPosition - offSet;
  } else {
    decodedCharPosition = encodedCharPosition + numOfLetters - offSet;
  }

  return alphabet[decodedCharPosition];
}

const testStrings = [
  "SERR PBQR PNZC",
  "SERR CVMMN!",
  "SERR YBIR?",
  "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.",
];
const solutions = testStrings.map(rot13);
console.log(solutions);

This works well in my environment

But I got a reference error in the freeCodeCamp site:
ReferenceError: i is not defined This comes from the line “for (i = 0; i < lowercaseString.length; i++)
which makes no sense.

Any help?

you need to declare all your variables. the i is not declared

freecodecamp uses strict mode, which you can switch on in your environment putting 'use strict' as first line of your js (quotes included)
with strict mode active you will get the same error in your environment


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums