我试图同时为两个数组创建一个循环,但是执行使我返回“ undefined”,我不明白为什么...请您看看我的代码并向我展示一下方法吗?
var questions = ["a", "b", "c"];
var responses = [5, 45, "test"];
var correctionList = "";
for (var i = 0; i < questions.length; i++); {
correctionList += questions[i] + ' : ' + responses[i];
var newLi = document.createElement('li');
var liContent = document.createTextNode(correctionList);
newLi.appendChild(liContent);
console.log(correctionList);
var showCorrection = document.querySelector('#correction-list');
showCorrection.appendChild(newLi);
console.log(liContent);
}
<ol id="correction-list"></ol>
我希望列表中包含“问题:答复”
undefined
您最后看到的可能是由于缺少函数return语句。仅当您在浏览器控制台上打印某些内容时,才会发生这种情况。
只需重构一下代码即可以问题:响应的格式显示问题列表和答案
function createList() {
var questions = ["a", "b", "c"];
var responses = [5, 45, "test"];
for (var i = 0; i < questions.length; i++) {
// Changed this line
var correctionList = questions[i] + ' : ' + responses[i];
var newLi = document.createElement('li');
var liContent = document.createTextNode(correctionList);
newLi.appendChild(liContent);
var showCorrection = document.querySelector('#correction-list');
showCorrection.appendChild(newLi);
}
createList();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句