-3

I'm trying to make an AtBash cipher but the for loop in AtBash()

var i = 0;

function ReverseString(s) { //FOR THE LOVE OF GOD .reverse() NEEDS TO WORK WITH STRINGS!
  var o = '',
    i = s.length;

  while (i--) {
    o += s.charAt(i);
  }

  return o;
}

function AtBash(input1) {
  var EncodedString = "";
  var alphabet = "abcdefghijklmnopqrstuvwxyz";
  var ReversedAlphabet = ReverseString(alphabet);

  for (i = 0; i < input1.lenght; i++) {
    var CurrentLetter = input1.indexOf(i);
    console.log(CurrentLetter); //To test if for loop runs
    var EncodedLetter = ReversedAlphabet.charAt(CurrentLetter);
    console.log(EncodedLetter); //To test if for loop runs
    EncodedString = EncodedString + EncodedLetter;
  }

  return EncodedString;
}

console.log("Test1");
console.log(AtBash("Test1"));

console.log("Test2");
console.log(AtBash("Test2"));

console.log("Gvhg3");
console.log(AtBash("Gvhg3"));

My question is what is wrong with my code? Is it because I'm calling my function wrong or returning nothing?

Aayan L
  • 77
  • 1
  • 6
  • 4
    Get in the habit of properly declaring your variables `for (var i = 0`. – Andy Feb 26 '16 at 02:17
  • 1
    Your reverse implementation is naive. See [here](http://stackoverflow.com/a/16776621/2505965). – Oka Feb 26 '16 at 02:28
  • another way to get a letter from a string is `string.charAt(letterPosition)`. – Ethan JC Rubik Feb 26 '16 at 04:56
  • @EthanJCRubik i saw my error there but thanks for helping, I ended up getting the charAt for the currentletter and taking a index of it from the alphabet and using that number for the reversed alphabet – Aayan L Feb 26 '16 at 05:01

1 Answers1

2

It looks like you have a typo when finding the length.

for (i = 0; i < input1.lenght; i++) {

Should be:

for (i = 0; i < input1.length; i++) {
Matt Elliott
  • 167
  • 1