gpt4 book ai didi

javascript - 我不明白为什么我的 for 循环返回 "undefined"

转载 作者:行者123 更新时间:2023-11-28 03:25:23 24 4
gpt4 key购买 nike

我试图同时为两个数组创建一个循环,但执行返回“未定义”,我不明白为什么......你能看一下我的代码并向我展示方式 ?

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>

我期望一个包含“问题:回应”的列表

最佳答案

您看到的最后一个未定义可能是因为缺少函数返回语句。仅当您在浏览器控制台上打印某些内容时才会发生这种情况。

只是稍微重构了代码,以问题:响应的格式显示问题和答案列表

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();

关于javascript - 我不明白为什么我的 for 循环返回 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com