2

所以我正在完成我的 codeacademy Javascript 课程。在这个特定的问题中,我通过 for 循环运行一个数组,并对每个数组项使用一个句子(5 次不同的时间)。我不知道我的语法有什么问题,但它在说ReferenceError: Invalid left-hand side expression in postfix operation

var names = ["Princilla, Afia, Tenesha, Marissa, Kalimah"];

for (i = 0; i < names.length; 1++) {
    console.log("I know someone called" + names[i]);
}
4

4 回答 4

4

A postfix operator is an operator (in this case, ++) that is placed after the operand (in this case, 1) on which it performs an operation. This error message is telling you that the value you are using as an operand is invalid.

This:

for (i = 0; i < names.length; 1++)

Should be this:

for (var i = 0; i < names.length; i++)

You want to increment the value of the i variable. You can't change the value of 1!

In addition, your array elements (or, in this case, element) probably aren't what you want them to be (as per Frits' answer).

于 2013-06-11T17:36:43.140 回答
0

您不能使用常量来添加后缀:1++

于 2013-06-11T17:38:02.123 回答
0

Also

var names = ["Princilla, Afia, Tenesha, Marissa, Kalimah"];

should be:

var names = ["Princilla", "Afia", "Tenesha", "Marissa", "Kalimah"];
于 2013-06-11T17:37:22.700 回答
0

问题在于您定义的数组以及1++

试试这个:- http://jsfiddle.net/aiioo7/zWh2S/

JS:-

var names = ["Princilla", "Afia", "Tenesha", "Marissa", "Kalimah"];

for (i = 0; i < names.length; i++) {
   console.log("I know someone called " + names[i]);
}
于 2013-06-11T17:40:56.290 回答