2 回答
TA贡献1842条经验 获得超21个赞
如果使用变量来访问数组中的数据,则可以大大简化代码 - 您不必对代码中的每个索引进行硬编码。我想你希望你的代码像这样工作:
var questions;
questions = ['artistic', 'daring', 'good', 'sports-minded', 'messy', 'disorganized', 'studious', 'funny', 'impacient', 'intelligent', 'neat', 'patient', 'lazy', 'shy', 'serious', 'nice', 'sociable', 'talented', 'hardworking', 'boy', 'girl', 'male friend', 'female friend', 'I', 'he', 'she', 'very', 'according to my family'];
var answers = ['artístico, artística', 'atrevido', 'bueno, buena', 'deportista', 'desordenado, desordenada', 'estudioso, estudiosa', 'gracioso, graciosa', 'impaciente', 'intelligente', 'ordenado, ordenada', 'paciente', 'perezoso, perezosa', 'reservado, reservada', 'serio, seria', 'simpático, simpática', 'sociable', 'talentoso, talentosa', 'trabajador, trabajadora', 'el chico', 'la chica', 'el amigo', 'la amiga', 'yo', 'él', 'ella', 'muy', 'según mi familia'];
//Your code doesnt have to check every single index, just use a variable
var display = document.getElementById('word');
var next = document.getElementById('next');
let i = 0;
function flip(idx) {
display.innerHTML = answers[idx];
if (i < answers.length - 1) {
//Increment the counter by 1 on each flip
i += 1;
} else {
//Back to first card
i = 0;
}
}
//Flip the first card right away
flip(0);
//Add an event handler to the button to call flip when the button is clicked
next.addEventListener('click', () => {
flip(i);
});
<input type="button" id="next" value="Next">
<div id="word"></div>
添加回答
举报