gpt4 book ai didi

javascript - 如果高于分配的变量javascript,则删除用户输入

转载 作者:行者123 更新时间:2023-11-30 10:03:52 24 4
gpt4 key购买 nike

我是一名学习 HTML/CSS/Javascript 的新程序员,一直在摆弄它,直到遇到一个错误。我让计算机猜测我的数字 (0-5),但后来意识到如果我输入的数字大于 5,网站就会崩溃。有什么办法可以做到,如果用户输入的数字大于 5,它会自动删除它吗?或者那不是 Javascript。提前致谢:)

document.getElementById("guess").onclick=function() {

var gotit=false;
var guesses=1;
var x;

while(gotit==false) {

x=Math.random();
x=6*x;
x=Math.floor(x);

if(document.getElementById("myNumber").value==x) {
gotit=true;
} else {
guesses++;
}
}

alert("Got it! It was a " + x + ". It only took me " + guesses + " guesses!");

}

最佳答案

试试这个:

 document.getElementById("guess").onclick=function() {      
if(document.getElementById("myNumber").value > 5) {
document.getElementById("myNumber").value = "";
alert("Please provide a number that is with in 0 to 5");
} else {
var gotit=false;
var guesses=1;
var x;

while(gotit==false) {

x=Math.random();
x=6*x;
x=Math.floor(x);

if(document.getElementById("myNumber").value==x) {
gotit=true;
} else {
guesses++;
}
}

alert("Got it! It was a " + x + ". It only took me " + guesses + " guesses!");
}

关于javascript - 如果高于分配的变量javascript,则删除用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30317720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com