基本上我有两个数组,一个包含 1-30 法语,另一个包含 1-30 英语。我一直在尝试做的是检查用户是否为法语号码选择了正确的翻译。这是以选择菜单的形式完成的。5个随机数用于从英语单词数组中挑选5个英语单词,然后将它们放入选择菜单中。在我点击提交猜测的那一刻,它会说每一个英文翻译都和我的法语单词一样!
下面的代码:
number = random = Math.floor((Math.random() * 30));
round = Math.round(number);
number = round;
function wordGen()
{
var RanNumbers = new Array(6); //Holds the generated Numbers.
for (var j = 0; j < RanNumbers.length; j++)
{
var temp = 0;
do
{
temp = Math.floor(Math.random() * 30);
}while (RanNumbers.indexOf(temp) > -1)
RanNumbers[j] = temp;
}
//Instead give user option of 5 answers to choose from
//Only one answer can be true.
foreignWords = new Array('un', 'deux', 'trois','quatre', 'cinq', 'six', 'sept','huit', 'neuf','dix','onze', 'douze', 'treize', 'quatorze','quinze','seize', 'dix-sept', 'dix-huit', 'dix-neuf', 'vingt', 'vingt et un','vingt-deux', 'vingt-trois', 'vingt-quatre', 'vingt-cinq', 'vingt-six', 'vingt-sept', 'vingt-huit', 'vingt-neuf', 'trente');
var translate = new Array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty-one', 'twenty-two', 'twenty-three', 'twenty-four', 'twenty-five', 'twenty-six', 'twenty-seven', 'twenty-eight', 'twenty-nine', 'thirty');
output = '';
randomWord = foreignWords[number];
correctAns = translate[number];
document.getElementById('generatedWord').innerHTML = randomWord;
var guessed = document.getElementById('guessed').value;
var guess = "<select name='guesses' id='guesses'>";
for(var i = 0; i < 6; i++)
{
guess += "<option value='" + i + "'>" + translate[RanNumbers[i]] + "</option>";
}
guess += '<option value="6">'+correctAns+'</option';
guess += "</select>";
document.getElementById('output').innerHTML = guess;
numGuessed = document.getElementById('guesses').value;
//What I have tried ATM, THis will say every answer is correct!
document.getElementById('submitAns').onclick = function(){
if(foreignWords.indexOf(number) === correctAns.indexOf(number)){
alert("Correct");
}
else{
alert('Incorrect');
}
}
}
我在下面为我的代码添加了 html:
<div data-role="page" id="page">
<div data-role="header">
<h1>Page One</h1>
</div>
<div data-role="content">
<h2 style="color:rgba(0,153,204,1);">Guess what the generated french word translates to in English!</h2><br />
<p align="center" id="generatedWord"></p>
<input type="button" value="Generate Word" onClick="wordGen();" />
<br />
<input type="text" id="guessed" />
<div align="center" id="output"></div>
<br />
<input type="button" id="submitAns" value="Submit Guess" />
</div>
所以目前我的 if 语句说每个英文单词都是正确的答案。在我的 HTML 页面中,我只有 2 个按钮,一个用于运行功能(显示法语单词并选择菜单),另一个按钮用于提交猜测。***如果我需要使用 jquery,我会,但我更喜欢使用纯 javascript!