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?