【发布时间】:2018-03-27 11:46:19
【问题描述】:
在我正在上课的课程中,他们举了一个使用forEach() 循环编辑数组内容的示例。
类示例:
var donuts = ["jelly donut", "chocolate donut", "glazed donut"];
donuts.forEach(function(donut) {
donut += " hole";
donut = donut.toUpperCase();
console.log(donut);
});
Prints:
JELLY DONUT HOLE
CHOCOLATE DONUT HOLE
GLAZED DONUT HOLE
问题是当我尝试使用相同的技术解决测验问题时,它不会更改数组值。我相信这与if 声明有关,但他们要求我们使用它,那么他们为什么不告诉我们有问题呢?
我的代码:
/*
* Programming Quiz: Another Type of Loop (6-8)
*
* Use the existing `test` variable and write a `forEach` loop
* that adds 100 to each number that is divisible by 3.
*
* Things to note:
* - you must use an `if` statement to verify code is divisible by 3
* - you can use `console.log` to verify the `test` variable when you're finished looping
*/
var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
];
test.forEach(function(element){
if (element % 3 === 0){
element += 100;
return element
}
});
console.log(test);
我尝试过运行return 语句,但没有运气。我联系了他们的“实时帮助”,但他们的帮助不大。谁能告诉我这里没有看到什么?
【问题讨论】:
-
你不应该使用
map吗?forEach在这种情况下不合适。donut通过值而不是引用传递给回调函数。编辑donut只会编辑该字符串的本地副本。如果donut是一个对象,你实际上可以改变它,因为它们是通过引用传递的。类的示例也不会更改原始数组。 -
returninforEach什么都不做……无处可归 -
map也不会修改数组,它会创建一个新数组。 -
我只是按照他们告诉我的去做。我只是跳过这个并删除问题。感谢您指出这一点!他们在这里并没有很清楚地问他们想要什么。
-
你可以留给未来的人学习。
标签: javascript arrays