我试图创造一个猜测游戏"使用javascript。我是JavaScript的新手,似乎无法让它发挥作用。
var theNumber = Math.round(50*Math.random());
function pickNumber ()
{
theNumber = Math.round(50*Math.random());
}
function compare ()
{
var choice = 0;
// Write a three-way conditional that compares choice to
// theNumber and displays the correct message.
}
比较功能应该是我需要编辑的唯一功能。下面我添加了我试图操作的HTML表单。
<form name="controls">
<select name="guess" onchange="compare();">
<option value=0> 0 </option>
<option value=1> 1 </option>
<option value=2> 2 </option>
<option value=3> 3 </option>
<option value=4> 4 </option>
<option value=5> 5 </option>
<option value=6> 6 </option>
<option value=7> 7 </option>
<option value=8> 8 </option>
<option value=9> 9 </option>
<option value=10> 10 </option>
</select>
<p>
<input type=text size=50 name="response">
</p>
<p>
<input type=button
value="Pick New Number"
onclick="pickNumber();">
</p>
</form>
答案 0 :(得分:0)
function compare(choice){
if (choice===theNumber){return "Win"}
else if(choice >= theNumber){return "Lower")
else {return "Higher"}
}
答案 1 :(得分:0)
<强> JS 强>
var theNumber = Math.round(50*Math.random());
function pickNumber ()
{
theNumber = Math.round(50*Math.random());
}
function compare ()
{
var val = document.getElementById('sel').value;
if(val === theNumber) {
document.getElementById('response').value = "Chosen Number is correct";
}
else if(val < theNumber){
{
document.getElementById('response').value = "Chosen Number is smaller";
} else {
document.getElementById('response').value = "Chosen Number is greater";
}
}
<强> HTML 强>
<form>
<select id = "sel"name="guess" onchange="compare()">
<option value=0> 0 </option>
<option value=1> 1 </option>
<option value=2> 2 </option>
<option value=3> 3 </option>
<option value=4> 4 </option>
<option value=5> 5 </option>
<option value=6> 6 </option>
<option value=7> 7 </option>
<option value=8> 8 </option>
<option value=9> 9 </option>
<option value=10> 10 </option>
</select>
<p>
<input id = "response" type=text size=50 name="response">
</p>
<p>
<input type=button
value="Pick New Number"
onclick="pickNumber()">
</p>
</form>