gpt4 book ai didi

javascript - 数字数组在字母数组之前加载

转载 作者:行者123 更新时间:2023-11-30 09:42:42 25 4
gpt4 key购买 nike

我有一个测验。这是一系列问题。该程序应该按顺序循环遍历它们。但是,它首先遍历所有带有数字的数组,然后是带有字母的数组,而不是我放入它们的顺序。以下是问题:

allQuestions = {
'358670': ['358670', '967723', '214571', '569420', '630697', 0],
'mbdpf': ['ajcwh', 'xirgb', 'dzelv', 'mbdpf', 'xguqx', 3],
'637592': ['348335', '921186', '637592', '188551', '391500', 2],
'xhtjv': ['xhtjv', 'jneez', 'cthul', 'bulwl', 'kqfwc', 0],
'206471': ['206471', '419643', '366549', '871328', '926142', 0],
'mwdif': ['bzvai', 'kslgq', 'futgf', 'mwdif', 'sikyp', 3],
'980924': ['327151', '242777', '708582', '860616', '980924', 4],
'usiyi': ['iyfod', 'lapwg', 'dqmtt', 'dyvwk', 'usiyi', 4],
'768898': ['808547', '689143', '875754', '768898', '872606', 3],
'ziojg': ['xqdiv', 'cyqsu', 'akoed', 'obtpn', 'ziojg', 4]
};

这是 codepen这是整个 JS:

window.onload = function() {

var questionArea = document.getElementsByClassName('questions')[0],
answerArea = document.getElementsByClassName('answers')[0],
checker = document.getElementsByClassName('checker')[0],
current = 0,

allQuestions = {
'358670': ['358670', '967723', '214571', '569420', '630697', 0],
'mbdpf': ['ajcwh', 'xirgb', 'dzelv', 'mbdpf', 'xguqx', 3],
'637592': ['348335', '921186', '637592', '188551', '391500', 2],
'xhtjv': ['xhtjv', 'jneez', 'cthul', 'bulwl', 'kqfwc', 0],
'206471': ['206471', '419643', '366549', '871328', '926142', 0],
'mwdif': ['bzvai', 'kslgq', 'futgf', 'mwdif', 'sikyp', 3],
'980924': ['327151', '242777', '708582', '860616', '980924', 4],
'usiyi': ['iyfod', 'lapwg', 'dqmtt', 'dyvwk', 'usiyi', 4],
'768898': ['808547', '689143', '875754', '768898', '872606', 3],
'ziojg': ['xqdiv', 'cyqsu', 'akoed', 'obtpn', 'ziojg', 4]
};


function loadQuestion(curr) {

var question = Object.keys(allQuestions)[curr];

questionArea.innerHTML = '';
questionArea.innerHTML = question;
}

function loadAnswers(curr) {

var answers = allQuestions[Object.keys(allQuestions)[curr]];

answerArea.innerHTML = '';

for (var i = 0; i < answers.length -1; i += 1) {
var createDiv = document.createElement('div'),
text = document.createTextNode(answers[i]);

createDiv.appendChild(text);
createDiv.addEventListener("click", checkAnswer(i, answers));


answerArea.appendChild(createDiv);
}
}

function checkAnswer(i, arr) {

return function () {
var givenAnswer = i,
correctAnswer = arr[arr.length-1];

if (givenAnswer === correctAnswer) {
addChecker(true);
} else {
addChecker(false);
}

if (current < Object.keys(allQuestions).length -1) {
current += 1;

loadQuestion(current);
loadAnswers(current);
} else {
questionArea.innerHTML = 'Done!';
answerArea.innerHTML = '';
}

};
}

function addChecker(bool) {

var createDiv = document.createElement('div'),
txt = document.createTextNode(current + 1);

createDiv.appendChild(txt);

if (bool) {

createDiv.className += 'correct';
checker.appendChild(createDiv);
} else {
createDiv.className += 'false';
checker.appendChild(createDiv);
}
}

loadQuestion(current);
loadAnswers(current);

};

最佳答案

您正在遍历一个对象,而不是一个数组。对象键的顺序不固定 - 如果需要特定顺序,请使用数组:

var obj = {
a: 'a',
1: 1,
x: 'x'
}
console.log('object keys', Object.keys(obj))

var arr = ['a', 1, 'x']
console.log('array values', arr)

关于javascript - 数字数组在字母数组之前加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452145/

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