gpt4 book ai didi

javascript - 如何使用 isNaN 来检查输入是否为数字?

转载 作者:行者123 更新时间:2023-11-28 04:42:29 25 4
gpt4 key购买 nike

我想限制代码中的符号、负数和所有其他非相关内容。我的教授告诉我使用 isNaN 但我不知道如何在我的代码中应用。有人可以帮我解决这个问题吗?我在下面附上了我的代码

<!-- Network Latency Calculator -->

<html>
<head>
<meta charset = "utf-8">
<title>Network Latency Calculation</title>
<script>

var firstNumber; // first string entered by user
var secondNumber; // second string entered by user
var thirdNumber; // third string entered by user
var fourthNumber; // fourth string entered by user
var number1; // first number to add
var number2; // second number to add
var number3; // third number to add
var number4; // fourth number to add
var sum; // sum of number1 and number2 and number3 and number4
var sumresult;




// read in first number from user as a string
firstNumber = window.prompt( "Enter the Propagation time (no negative number, letter, blank space, and symbol is not allow)" );

// read in second number from user as a string
secondNumber = window.prompt( "Enter the Transmission time (no negative number, letter, blank space, and symbol is not allow)" );


// read in third number from user as a string
thirdNumber = window.prompt( "Enter the Queuing time (no negative number, letter, blank space, and symbol is not allow)" );


// read in fourth number from user as a string
fourthNumber = window.prompt( "Enter the Propagation delay (no negative number, letter, blank space, and symbol is not allow)" );








// convert numbers from strings to integers
number1 = parseFloat( firstNumber );
number2 = parseFloat( secondNumber );
number3 = parseFloat( thirdNumber );
number4 = parseFloat( fourthNumber );

sum = number1 + number2 + number3 + number4; // add the numbers

sumresult = parseFloat( sum );


// display the results
document.writeln( "<h1>The network latency is " + sum + "</h1>" );
document.writeln( "<p>Propagation time " + number1 + "</p>" );
document.writeln( "<p>Transmission time " + number2 + "</p>" );
document.writeln( "<p>Queuing time " + number3 + "</p>" );
document.writeln( "<p>Propagation delay " + number4 + "</p>" );

if ( sumresult < 1 )
document.writeln( "<p>Network performance is good.</p>" );
else ( document.writeln( "<p>Warning: Network performance needs improvement.</p>" ));






</script>
</head><body></body>

最佳答案

在生产 Web 应用程序中,您不会提示用户输入数字 - 您会显示一个表单供用户填写。

但是,由于我们正在使用玩具应用程序,因此您可以简单地重复提示,直到用户正确理解为止:

function getNumberFromUser(promptText) {
var n = NaN;
while(isNaN(n) || (n < 0)) {
var nString = window.prompt(promptText);
n = parseFloat(nString);
}
return n;
}
var number1 = getNumberFromUser("Enter the Propagation time (no negative number, letter, blank space, and symbol is not allow)");
var number2 = getNumberFromUser("Enter the Transmission time (no negative number, letter, blank space, and symbol is not allow)");
//etc

关于javascript - 如何使用 isNaN 来检查输入是否为数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646647/

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