Javascript - 遍历数组
Javascritpt - Looping through an array
所以我刚开始学js,做不了这个题:
Create a file named looping-through-arrays.js
.
In that file, define a variable named pets that references this array:
['cat', 'dog', 'rat']
Create a for loop that changes each string in the array so that they
are plural.
You will use a statement like this inside the for loop:
pets[i] = pets[i] + 's'
我试过类似这样的代码,但显然它不起作用:
let pets = ["cat", "dog", "rat"];
for(let i = 0; i <= pets.length; i++){
pets[i] = pets[i] + "s";
};
console.log(pets);
//Try this one
let pets = ["cat", "dog", "rat"];
for(let i = 0; i < pets.length; i++){
pets[i] = pets[i] + "s";
};
console.log(pets);
所以我刚开始学js,做不了这个题:
Create a file named
looping-through-arrays.js
.In that file, define a variable named pets that references this array:
['cat', 'dog', 'rat']
Create a for loop that changes each string in the array so that they are plural.
You will use a statement like this inside the for loop:
pets[i] = pets[i] + 's'
我试过类似这样的代码,但显然它不起作用:
let pets = ["cat", "dog", "rat"];
for(let i = 0; i <= pets.length; i++){
pets[i] = pets[i] + "s";
};
console.log(pets);
//Try this one
let pets = ["cat", "dog", "rat"];
for(let i = 0; i < pets.length; i++){
pets[i] = pets[i] + "s";
};
console.log(pets);