Javascript 如何分配命名数组?
如果这是个愚蠢的问题,请原谅,我还是个初学者。以下是练习:Javascript 如何分配命名数组?,javascript,arrays,Javascript,Arrays,如果这是个愚蠢的问题,请原谅,我还是个初学者。以下是练习: function exerciseOne(classmates){ // Exercise One: You will be given an array called 'classmates' // Create a variable called 'howManyStudents', and assign it to // the length of the classmates array (using
function exerciseOne(classmates){
// Exercise One: You will be given an array called 'classmates'
// Create a variable called 'howManyStudents', and assign it to
// the length of the classmates array (using the length property)
我的回答是:
let howManyStudents = classmates;
console.log(classmates.length);
当我试着运行测试时,它说预期答案['a','b','c','d','e']为5。如果它是['a'、'b'、'c'、'd'、'e'],我会很好,但它作为名称而不是数组的书写方式让我有点困惑
如果有人能告诉我我做错了什么,我会非常感激
编辑:成功了,非常感谢 您需要为变量指定classes.length属性
function exerciseOne(classmates){
let howManyStudents = classmates.length;
console.log(howManyStudents);
return howManyStudents
}
您没有将同学的长度指定给howManyStudents变量,而是将同学的数据指定给howManyStudents,而不是指定同学的长度 应该是:
let howManyStudents = classmates.length;
console.log(howManyStudents);