如何解决我不断遇到的 Javascript 错误?
我解决了这个问题。测试用例不断失败可能是因为需要实现一个 try catch 块。我完全忘记了这一点。当我添加 try catch 块时,测试用例有效。 感谢您的所有建议。 问题陈述:我需要设计一个简单的 html 表单,它将限制作为输入并显示斐波那契数列的第一个给定数字。 例如。如果给出 5 作为输入,则显示:0 1 1 2 3 如果给出 8 作为输入,则显示:0 1 1 2 3 5 8 13 但我不断收到此错误:
testFiboForNonZeroPositiveInput:
Check for the logic and check if the correct output is displayed in div with id 'result'
testFiboForZeroInput:
Check for the logic and check if the correct output is displayed in div with id 'result'
测试用例失败
这是我的代码:
function getFibonacci(){
var fib=document.getElementById("fibo").value;
var text;
var arr=[];
if (fib.length===0){
text="Please, specify a number.";
document.getElementById("result").innerHTML = text;
}
else if (fib<0){
text="Please, specify a positive number.";
document.getElementById("result").innerHTML = text;
}
else{
var n1 = 0, n2 = 1, nextTerm, i;
for (i = 1; i <= fib; i++) {
arr.push(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
var newStr = arr.join(' ').trim()
document.getElementById("result").innerHTML=newStr;
}
return false;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fibonacci Series</title>
<script src="script.js"></script>
</head>
<body>
<form onsubmit=" return getFibonacci()">
<label for="Enter the number to get a fibonacci">Enter the number to get a fibonacci</label>
<input type="number" id="fibo" name="fibo"><br>
<input type="submit" value="Get Fibonacci Numbers" id="fibobtn">
<div id="result"></div>
</form>
</body>
</html>
一切似乎都工作正常,我收到了斐波那契数列和所需的其他消息,但我的测试用例由于此错误而失败。请告诉我该怎么做才能解决这个问题。
I solved the issue. The test cases kept on failing probably because there was a try catch block that needed to be implemented. I completely forgot about that. The test cases worked when I added the try catch block.
Thanks for all the suggestions.
Problem Statement: I need to design a simple html form that takes a limit as input and displays the first given number of the Fibonacci series.
for eg. if 5 is given as input it displays: 0 1 1 2 3
if 8 is given as input it displays: 0 1 1 2 3 5 8 13
But I keep on getting this error:
testFiboForNonZeroPositiveInput:
Check for the logic and check if the correct output is displayed in div with id 'result'
testFiboForZeroInput:
Check for the logic and check if the correct output is displayed in div with id 'result'
TEST CASE FAILED
Here is my code:
function getFibonacci(){
var fib=document.getElementById("fibo").value;
var text;
var arr=[];
if (fib.length===0){
text="Please, specify a number.";
document.getElementById("result").innerHTML = text;
}
else if (fib<0){
text="Please, specify a positive number.";
document.getElementById("result").innerHTML = text;
}
else{
var n1 = 0, n2 = 1, nextTerm, i;
for (i = 1; i <= fib; i++) {
arr.push(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
var newStr = arr.join(' ').trim()
document.getElementById("result").innerHTML=newStr;
}
return false;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fibonacci Series</title>
<script src="script.js"></script>
</head>
<body>
<form onsubmit=" return getFibonacci()">
<label for="Enter the number to get a fibonacci">Enter the number to get a fibonacci</label>
<input type="number" id="fibo" name="fibo"><br>
<input type="submit" value="Get Fibonacci Numbers" id="fibobtn">
<div id="result"></div>
</form>
</body>
</html>
Everything seems to work fine and I get the Fibonacci series and the other messages as required but my test cases fail due to this error. Please tell me what to do to fix this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对代码进行了这些更改并且测试用例有效:
(添加了必需的 try catch 块)
I made these changes to the code and the test cases worked:
(Added a required try catch block)
label
上的for
属性必须引用 id。A
for
attribute onlabel
must by referencing to an id.