gpt4 book ai didi

javascript - 如何跟踪 JavaScript 中的计算错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:25 25 4
gpt4 key购买 nike

我正在开发一个网站,该网站可以动态创建表格并为 http://www.primenumbertable.com 的用户突出显示质数。 .我以为我已经正确地规划了计算流程,但是在我包含了让用户包含起点的能力之后,有些东西就崩溃了。

不是将表格计算到指定的范围,表格有时会被缩短。例如,如果用户指定他们要检查 100 个数字,并且起点为 1,则该表将只生成数字 1-9,而不是 1-100。

我还拿出了笔和pad,自己一步一步的计算,看看哪里出错了,但是我的pen和pad的计算没有出现计算异常。

在我看来,正在输入的值在进入表格之前以某种方式切断了最后一个“0”,但我无法找到这是如何发生的。然后,我认为这不是问题所在,因为当我尝试检查 99 个数字时,我得到的表格一直到 102!

所以,我真的有两个问题:1 - 我如何追踪发生计算错误的位置,以及 2 - 任何人都可以找到解决问题的方法吗?

以下是我的源代码:

<!DOCTYPE HTML>
<html lang="en">
<!-- This page asks the user to input a number to search for primes and returns a base-6 table with primes highlighted -->
<head>

<meta charset="utf-8">
<meta name="keywords" content="prime numbers, prime, primes, prime twins">
<title>Prime Numbers</title>

<script type="text/javascript">

/* This function determines if a number is prime or not */
function isPrime(n) {
if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false;
if (n%2==0) return (n==2);
if (n%3==0) return (n==3);
var m=Math.sqrt(n);
for (var i=5;i<=m;i+=6) {
if (n%i==0) return false;
if (n%(i+2)==0) return false;
}
return true;
}

/* This function creates the table and color codes the cells of prime numbers */
function tableCreate(X,Y){

var numCheck = X; /* assigns numCheck the value the user input for how many numbers are to be checked*/
var numRow = numCheck/6; /* assigns the value that will be used as the number of rows in the table. */
var numCell = Y; /* assigns the value that will be used in the table cell */
var body = document.body; /* assigns the value of the document body */
var tbl = document.createElement('table'); /* creates a table dynamically to be filled */
var highlightColor = document.getElementById('highlight').value; /* assign the value of the color the user requested for highlighting primes */

if(isNaN(numCheck)==true){
document.getElementById('tablearea').innerHTML='Please enter a number in arabic numerals.';
return; /* if the user did not enter a number to check, the function will instruct the user to do so properly. */
}
else{
for(var i = 0; i < numRow; i++){ /*continues the loop until the proper number of rows have been dynamically filled */
var tr = tbl.insertRow(-1); /* inserts a new row at the bottom */
for(var j = 0; j < 6; j++){ /*continues the loop until all data cells in the row have been filled */
if(i==numRow && j==6 || numCheck==(numCell + Y)-1){
break
}
else {
var td = tr.insertCell(-1); /* inserts a new data cell into the row */
if(isPrime(numCell) == true){ /* calls the function isPrime to test if the number going into the data cell is a prime */
if(highlightColor == ''){
td.style.backgroundColor='yellow'; /* uses the color yellow to highlight a cell if the user failed to specify a color */
}
else{
td.style.backgroundColor=highlightColor; /* uses the user's choice of color to highlight the cell if the number going in is prime */
}
}
td.appendChild(document.createTextNode(numCell)); /* prints the number in the data cell */
numCell++; /* increases the count of the number of numbers checked by 1 */
}
}
}
document.getElementById('tablearea').innerHTML=''; /* clears any previous tables that were written */
document.getElementById('tablearea').appendChild(tbl); /* writes the new table */
document.getElementById('backtotop').innerHTML="Back to top"; /* puts a 'back to top' link at the bottom */
document.getElementById('backtotop').href="#top"; /* links the btt link to the top */
}
}
/* This function clears the tablearea when called */
function tableClear(){
document.getElementById('tablearea').innerHTML='';
document.getElementById('backtotop').innerHTML='';
}
</script>

<style type="text/css">
body{
background-image: url('primesbackground.png');
}
h1{
width: 700px;
margin: auto;
text-align: center;
background-color: white;
}
table{
width: 50%;
border: 1px solid black;
background-color: gray;
position: relative;
}
td{
border: 1px solid black;
background-color: white;
}
.parameters{
float: left;
width: 300px;
height: 100%;
margin: 10px;
display: inline-block;
}
.tablearea{
text-align: center;
display: inline-block;
}
</style>

</head>

<body>
<a id="top"></a>
<h1>Welcome to the Prime Number Table Generator.</h1>
<div>
<div class="parameters">
<form name="primes">
<p style="text-align: center; margin: 10px">
How many numbers would you like to check for primes?<br />
<input type="text" id="nums"><br />
What number would you like to use as a starting point?<br />
<input type="text" id="startpoint"><br />
What color would you like primes highlighted?<br />
<input type="text" id="highlight"><br />
<input type="button" value="Check'em!" onClick="tableCreate(document.primes.nums.value,document.primes.startpoint.value)">
<input type="reset" onClick="tableClear()"><br />
To suggest improvements to this site, please send an email to&nbsp;<a href="mailto:webmaster@primenumbertable.com">webmaster@primenumbertable.com</a>.<br />
</p>
</form>
</div>
<div id="tablearea" class="tablearea">
<!-- This area is filled by the function tableCreate() -->
</div>
<p style="text-align: center; float: bottom"><a href="" id="backtotop"></a></p>
</div>
</body>
</html>

最佳答案

Chrome 也有开发者工具(按F12)

这似乎是您的 numRow 属性有问题。

var numRow = numCheck/6;

numCheck 为 100 时,这会将 numRow 设置为 float :16.666666666666668

稍后您将检查此值以中断您的 for 循环

if(i==numRow && j==6 || numCheck==(numCell + Y)-1){ 

但是 i 永远不会等于 numRow 因为 i 是一个整数而 numRow 是一个 float

我会尝试使用 parseInt 方法通过替换此行将 numCheck 解析为整数

var numRow = numCheck/6;

var numRow = parseInt(numCheck/6);

关于javascript - 如何跟踪 JavaScript 中的计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299871/

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