我正在创建一个测验,其中出现了随机生成的对象属性 (object.name),并且用户输入了一个文本值以匹配关联的对象属性 (object.number)。我有我的函数工作(当值匹配时返回“正确”,当它们不匹配时返回“不正确”),但我希望问题重复,直到用户得到正确的答案。现在,即使用户输入了不正确的值,随机生成一个新问题。你能帮我构造这个问题,让它重复相同的问题,直到用户输入匹配的属性值吗?
<body>
<h3 id="randThing"></h3>
<form name="userInputForm">
<input type="text" id="userIndexInput"></form>
<button type="button" id="enter" onClick="compareAnswer()">Validate</button>
<script type="text/javascript">
function object(name, value)
{
this.name=name;
this.value=value;
}
var car=new object("Car","50333");
var desk=new object("Desk","10420");
var monkey=new object("Monkey","11450");
var a = [car,desk,monkey];
var randomValue = a[Math.floor(a.length * Math.random())];
var specificThing = randomValue;
document.getElementById("randThing").innerHTML=specificThing.name;
var specificThingValue = specificThing.value;
function compareAnswer()
{
var x = document.getElementById("userIndexInput").value;
if (x === specificThingValue)
{
alert("Correct!");
document.getElementById("randThing").innerHTML=randomValue;
}
else
{
alert("Try Again!");
//don't know what to put here to make same the same specificThing generate again until x === specificThingValue
}
};
</script>