猿问

如何检查一个值是否与它在 Javascript 中的哈希键匹配

我正在做一个随机生成西班牙语单词的小项目,用户输入英文翻译,然后点击查看它是对还是错。目前它处于早期阶段,但我无法正确匹配逻辑。到目前为止,我有:


Spanish_Phrases.prototype.calculate = function(){

  const phrases = {

    hola: "hello",

    adios: "bye",

    bonita: "bonita",

  };

  this.answer = (document.getElementById("answer").value);


    if(this.answer == Object.values(phrases)) {

      alert("Correct")

  } else {

      alert("Try again");

  }

}

HTML在这里:


<!DOCTYPE html>

<html>

<head>

    <title>Spanish</title>

</head>

<body>

    <center>

        <script src="./src/spanish.js">

        </script>

        <link href="skeleton.css" rel="stylesheet">

        <section>

            <h2>Spanish</h2>

            <h3 id="question"></h3>

      <button type="button" id="questionbutton">Get question</button>

            <form action="#" id="form" name="form" onsubmit="return false;">

                Enter answer<br>

                <input id="answer" name="answer" type="text"><br>

                <br>

                <input id="submit" type="submit" value="Submit">

            </form>

        </section>

    </center>

    <script src="https://code.jquery.com/jquery-3.4.1.min.js">

    </script>

    <script>


  $(function () {


  var spanish = new Spanish_Phrases();


  $('#questionbutton').on('click', function(){

    spanish.randomize();

    $('#question').text(spanish.question);

  })

  $('#submit').on('click', function() {

    spanish.calculate();

  });

});


</script>

答案是用户在表单中提交的内容。如果该答案值正确匹配其相应的键,我希望它显示正确,但无法弄清楚如何。


抱歉,我很欣赏这可能是非常基础的,但我的 javascript 知识非常生疏。有人可以帮忙吗?


狐的传说
浏览 100回答 1
1回答

守着一只汪

你不应该知道你问的是哪个问题吗?只需检查它是否匹配。if&nbsp;(&nbsp;phrases[&nbsp;spanish.question&nbsp;]&nbsp;==&nbsp;answer&nbsp;)&nbsp;{&nbsp;...&nbsp;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答