我正在制作抽认卡游戏,但“下一步”按钮不起作用

时间:2019-09-05 22:34:03

标签: javascript html arrays

我正在制作一个“下一步”按钮以移至该游戏中的下一个抽认卡,但是当我单击该按钮时,什么也没有发生。我不知道我在做什么错。

我在这段代码中使用了很多变量,例如数字变量来说明问题和答案。我尝试每次都向数字变量添加一个,以将其更改为下一个问题,但是它不起作用。

SELECT
    TO_CHAR(
        SYSTIMESTAMP AT TIME ZONE 'UTC',
        'yyyy-mm-dd"T"hh24:mi:ss"Z"'
    )
FROM dual;

我想发生的事情是,当用户单击“下一步”按钮时,它将更改为下一个闪存卡。

2 个答案:

答案 0 :(得分:1)

该函数可能看起来像这样。顺便说一下,您在onlick="next()"

中有一个错字

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'];
var number = 0;
var answer = answers[number];
var word = questions[number];
var display = document.getElementById('word');
display.innerHTML = word;
function flip() {
  if (word == questions[0]) {
    display.innerHTML = answer;
  }
  if (word == questions[1]) {
    display.innerHTML = answer;
  }
  if (word == questions[2]) {
    display.innerHTML = answer;
  }
  if (word == questions[3]) {
    display.innerHTML = answer;
  }
  if (word == questions[4]) {
    display.innerHTML = answer;
  }
  if (word == questions[5]) {
    display.innerHTML = answer;
  }
  if (word == questions[6]) {
    display.innerHTML = answer;
  }
  if (word == questions[7]) {
    display.innerHTML = answer;
  }
  if (word == questions[8]) {
    display.innerHTML = answer;
  }
  if (word == questions[9]) {
    display.innerHTML = answer;
  }
  if (word == questions[10]) {
    display.innerHTML = answer;
  }
  if (word == questions[11]) {
    display.innerHTML = answer;
  }
  if (word == questions[12]) {
    display.innerHTML = answer;
  }
  if (word == questions[13]) {
    display.innerHTML = answer;
  }
  if (word == questions[14]) {
    display.innerHTML = answer;
  }
  if (word == questions[15]) {
    display.innerHTML = answer;
  }
  if (word == questions[16]) {
    display.innerHTML = answer;
  }
  if (word == questions[17]) {
    display.innerHTML = answer;
  }
  if (word == questions[18]) {
    display.innerHTML = answer;
  }
  if (word == questions[19]) {
    display.innerHTML = answer;
  }
  if (word == questions[20]) {
    display.innerHTML = answer;
  }
  if (word == questions[21]) {
    display.innerHTML = answer;
  }
  if (word == questions[22]) {
    display.innerHTML = answer;
  }
  if (word == questions[23]) {
    display.innerHTML = answer;
  }
  if (word == questions[24]) {
    display.innerHTML = answer;
  }
  if (word == questions[25]) {
    display.innerHTML = answer;
  }
  if (word == questions[26]) {
    display.innerHTML = answer;
  }
}

function next() {
  number++;
  answer = answers[number];
  word = questions[number];
  display.innerHTML = word;
} //this is the function I need help on.
@import url('https://fonts.googleapis.com/css?family=Patrick+Hand+SC&display=swap');
#flashcard{
  width:400;
  height:250;
  background:#fff555;
  position:relative;
}
#word{
  position:absolute;
  transform:translate(-50%,-50%);
  top:110px;
  left:50%;
  font-family: 'Patrick Hand SC', cursive;
}
<div id="flashcard" onclick="flip()"><h1 id="word"></h1></div>
<br>
<button onclick="next()">Next</button>

答案 1 :(得分:1)

如果使用变量访问数组中的数据,则可以大大简化代码-不必对代码中的每个索引进行硬编码。我认为您希望您的代码像这样工作:

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>