For loop in function似乎没有运行

时间:2016-02-26 02:13:38

标签: javascript

我试图制作一个AtBash密码但AtBash中的for循环()

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"));

我的问题是我的代码出了什么问题?是因为我调用了我的函数错误还是没有返回任何内容?

1 个答案:

答案 0 :(得分:2)

看起来你在查找长度时会有拼写错误。

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

应该是:

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