亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查一個值是否與它在 Javascript 中的哈希鍵匹配

如何檢查一個值是否與它在 Javascript 中的哈希鍵匹配

狐的傳說 2022-06-16 10:14:13
我正在做一個隨機生成西班牙語單詞的小項目,用戶輸入英文翻譯,然后點擊查看它是對還是錯。目前它處于早期階段,但我無法正確匹配邏輯。到目前為止,我有: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 知識非常生疏。有人可以幫忙嗎?
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

你不應該知道你問的是哪個問題嗎?只需檢查它是否匹配。

if ( phrases[ spanish.question ] == answer ) { ... }


查看完整回答
反對 回復 2022-06-16
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號