我正在 repl.it 上做一个关于 JavaScript 的测验,但由于某种原因,即使我很确定我的语法是正确的,我还是不断收到错误消息。我正在使用 JavaScript MDN 中的数组方法。
你能指出我的错误吗?
问题:将项目添加到数组的前面。我的代码:
function addItemToFront(arr, item) {
// code here
const newItem = [1, 2, 3];
newItem.unshift(0);
return newItem;
}
addItemToFront(newItem);
问题:testScores 是一个数组。迭代 testScores 并计算并返回平均值。
我的代码:
function averageTestScore(testScores) {
// code here
testScores = [97, 100, 80, 55, 72, 94];
let count = count.length;
scores = scores.reduce((previous, current) => current += previous);
scores /= count;
return scores;
}
averageTestScores(testScores);
问题:如果 num 在 arr 内,则返回 true,否则返回 false。
我的代码:
function contains(arr, item) {
// code here
const string = ['hello', 'world'];
if(string.includes('hello')){
return true
} else {
return false;
}
}
contains('hello');
问题:words 是一个字符串数组。返回一个由所有单词连接在一起的字符串。
我的代码:
function wordsToSentence(words) {
// code here
words = ['hello', 'world', 'computer', 'science', 'Lambda'];
words.join(' ');
return words;
}
wordsToSentence(words);
所有四种解决方案都有相同的问题。
您正在使用错误的、不存在的参数调用函数。第一部分甚至不需要参数(它们没有被使用)。
第二个,调用带有未定义变量的方法。
第三个,忽略传递的变量,所以你的代码不能真正工作。
第四个,调用带有未定义变量的方法。
伙计,这可能是一些功课,在我看来,你应该真正尝试解决这个问题,因为你的代码有很多问题。
如果您想找出错误,请查看 Chrome 控制台。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句