0

I'm working on a small project where a random Spanish word is generated, the user inputs the English translation, and they click to see if it's right or wrong. At the moment its at the early stages, but I cannot get the matching logic correct. So far I have:

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");
  }
}

The HTML is here:

<!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>

The answer is what the user has submitted in the form. I want it to show correct if that answer value correctly matches its corresponding key, but cannot work out how.

Sorry I appreciate this is potentially quite basic but my javascript knowledge is very rusty. Can anyone help please?

juemura7
  • 411
  • 9
  • 20
  • Object.values returns an array and you're compairing an array with a string. – Code Maniac Apr 05 '20 at 12:50
  • 1
    [Related, but not exactly duplicate](https://stackoverflow.com/questions/61042479/how-to-get-a-random-key-value-from-a-javascript-object). Knowing, that people always get triggered, i'll be slightly harsh again: imho, working on your fundamentals (reading another long javascript tutorial, doing some easy coding challenges, and afterwards, reading what related functions do), would help everyone involved the most. You are programming by trial-and-error, which will almost always end in disaster earlier or later. – ASDFGerte Apr 05 '20 at 12:53

1 Answers1

0

Shouldn't you know which question you've asked? Just check if it matches.

if ( phrases[ spanish.question ] == answer ) { ... }
Peri
  • 574
  • 1
  • 3
  • 19