我对此是全新的,因此,如果我没有按原意解释问题,请告诉我!
我基本上是在使用Twilio Quest作为开始学习Javascript的一种方式,并且使自己陷入了困境。
挑战在于测试字符串数组的条件,并在每次出现某个字符串时增加变量的值……然后在函数末尾返回所述变量的值。
这是我所拥有的:
let freightItems = ['contraband', 'clear', 'contraband', 'clear'];
freightItems.forEach(scan);
function scan(freightItems) {
const contrabandCount = 0;
if (freightItems.element == 'contraband') {
contrabandCount + 1;
}
return contrabandCount;
}
将代码提交给TwilioQuest时遇到的错误是:
您的函数返回了一个数字,但没有返回我们想要的值。您的函数应检查输入数组中的每个项目,并返回字符串“违禁品”出现的总次数。
您的代码中的几个问题:
您应该遍历scan
函数内部的数组,因为您不能contrabandCount
像尝试那样返回变量。forEach
在scan
函数内部移动循环
更改
contrabandCount + 1;
至
contrabandCount = contrabandCount + 1;
因为您需要contrabandCount
使用以下结果更新变量contrabandCount + 1
let freightItems = ['contraband', 'clear', 'contraband', 'clear'];
function scan(freightItems) {
let contrabandCount = 0;
freightItems.forEach(str => {
if (str === 'contraband') {
contrabandCount = contrabandCount + 1;
}
})
return contrabandCount;
}
console.log(scan(freightItems))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句