JS数组中的两个正确答案

时间:2018-11-13 16:39:27

标签: javascript arrays

我正在用不规则动词创建波兰测验,但我遇到了一个问题,例如,输入中的“ be”一词与“ was”和“ were”两个词匹配适合2个字?

const foo = () => {
  var words = [
  {INF: 'be', 	SIM: 'was',	PAR: 'been',	PL: 'być',	},
  {INF: 'begin',	SIM: 'began',	PAR: 'begun',	PL: 'zaczynać'},
  {INF: 'break',	SIM: 'broke',	PAR: 'broken',	PL: 'łamać'},
  {INF: 'bring',	SIM: 'brought',	PAR: 'brought',	PL: 'przynosić'},
  {INF: 'buy',  SIM: 'bought',	PAR: 'bought',	PL: 'kupować'},
  {INF: 'build',	SIM: 'built',	PAR: 'built',	PL: 'budować'},
  {INF: 'choose',	SIM: 'chose',	PAR: 'chosen',	PL: 'wybierać'},
  {INF: 'come',	SIM: 'came',	PAR: 'come',	PL: 'przyjść'},
  {INF: 'cost',	SIM: 'cost',	PAR: 'cost',	PL: 'kosztować'},
  {INF: 'cut',	SIM: 'cut',	PAR: 'cut',	PL: 'ciąć'},
  ]

  const word = document.getElementById('word');
	const test = document.getElementById('test');
  const InputInf = document.getElementById('InputInf');
  const InputSim = document.getElementById('InputSim');
  const InputPar = document.getElementById('InputPar');
	const submit = document.getElementById('submit');

  let randomWord = null;

  const num = () => Math.floor(Math.random() * 10);
  const postWord = () => { randomWord = word.innerText = words[num()].PL; }

  postWord();

  submit.addEventListener('click', () => {
    const inputValueInf = InputInf.value;
    const inputValueSim = InputSim.value;
    const inputValuePar = InputPar.value;
    const matchingInf = words.find(word => word.PL === randomWord).INF;
    const matchingSim = words.find(word => word.PL === randomWord).SIM;
    const matchingPar = words.find(word => word.PL === randomWord).PAR;

    if (inputValueInf === matchingInf  && inputValueSim === matchingSim  &&
    inputValuePar === matchingPar) {
    	postWord();
      	InputInf.value = '';
    		InputSim.value = '';
    		InputPar.value = '';
        answerInf.innerText = '';
        answerSim.innerText = '';
        answerPar.innerText = '';
      return;
    }

    if(inputValueInf !== matchingInf){
        answerInf.textContent = "[ "+matchingInf+" ]";
          document.getElementById("answerInf").className = "answer";
      }   else{
            document.getElementById("answerInf").className = "display-none";
      }

    if(inputValueSim !== matchingSim){
        answerSim.textContent = "[ "+matchingSim+" ]";
          document.getElementById("answerSim").className = "answer";
      }   else{
            document.getElementById("answerSim").className = "display-none";
      }

    if(inputValuePar !== matchingPar){
        answerPar.textContent = "[ "+matchingPar+" ]";
          document.getElementById("answerPar").className = "answer";
      }   else{
            document.getElementById("answerPar").className = "display-none";
      }
    }, false);
};

window.onload = foo();
<div id="quiz">
<div id="word">Zaczynać</div>
<input type="text" id="InputInf" class="quiz-input" placeholder="Infinitive">
<input type="text" id="InputSim" class="quiz-input" placeholder="Simple Past">
<input type="text" id="InputPar" class="quiz-input" placeholder="Past Participle">

<input type="submit" id="submit" class="submit" value="Check">

<div class="ugh">
  <div id="answerInf" class="answer"></div>
  <div id="answerSim" class="answer"></div>
  <div id="answerPar" class="answer"></div>
</div>

如果有人喜欢jsFiddle:code

1 个答案:

答案 0 :(得分:0)

您可以通过将字段设置为包含正确答案的数组来实现。 这是一个示例:

  var words = [
  {INF: 'be', 	SIM: ['was', 'were'],	PAR: 'been',	PL: 'być',	},
  {INF: 'begin',	SIM: 'began',	PAR: 'begun',	PL: 'zaczynać'},
  {INF: 'break',	SIM: 'broke',	PAR: 'broken',	PL: 'łamać'},
  {INF: 'bring',	SIM: 'brought',	PAR: 'brought',	PL: 'przynosić'},
  {INF: 'buy',  SIM: 'bought',	PAR: 'bought',	PL: 'kupować'},
  {INF: 'build',	SIM: 'built',	PAR: 'built',	PL: 'budować'},
  {INF: 'choose',	SIM: 'chose',	PAR: 'chosen',	PL: 'wybierać'},
  {INF: 'come',	SIM: 'came',	PAR: 'come',	PL: 'przyjść'},
  {INF: 'cost',	SIM: 'cost',	PAR: 'cost',	PL: 'kosztować'},
  {INF: 'cut',	SIM: 'cut',	PAR: 'cut',	PL: 'ciąć'},
  ];
  
  var userAnswer = "was";
  var ifn = "be";
  
  var words = words.find(word => word.INF === ifn);
  
  var isCorrect = Array.isArray(words.SIM) ? words.SIM.indexOf(userAnswer) > -1 : words.SIM == userAnswer;
  
  console.log(isCorrect);