如何解决我不断遇到的 Javascript 错误?

发布于 2025-01-16 03:54:11 字数 2267 浏览 0 评论 0原文

我解决了这个问题。测试用例不断失败可能是因为需要实现一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2025-01-23 03:54:11

我对代码进行了这些更改并且测试用例有效:
(添加了必需的 try catch 块)

function getFibonacci(){
    try{
    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 if(fib>0){
    var num1=0; 

    var num2=1; 

    var nextterm; 

    var i=0; 

    for (i = 0; i < fib; i++)  

    { 
        arr.push(num1);
        nextterm=num1+num2; 

         num1=num2; 

         num2=nextterm; 
    
     }
     var newStr = arr.join(' ');
     document.getElementById("result").innerHTML=newStr;
     }else {
            text="0";
            document.getElementById("result").innerHTML = text;
    
    
    }
    
    }
    catch(err){
        document.getElementById("result").innerHTML=err;
    }
    return false;
}

I made these changes to the code and the test cases worked:
(Added a required try catch block)

function getFibonacci(){
    try{
    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 if(fib>0){
    var num1=0; 

    var num2=1; 

    var nextterm; 

    var i=0; 

    for (i = 0; i < fib; i++)  

    { 
        arr.push(num1);
        nextterm=num1+num2; 

         num1=num2; 

         num2=nextterm; 
    
     }
     var newStr = arr.join(' ');
     document.getElementById("result").innerHTML=newStr;
     }else {
            text="0";
            document.getElementById("result").innerHTML = text;
    
    
    }
    
    }
    catch(err){
        document.getElementById("result").innerHTML=err;
    }
    return false;
}
呆° 2025-01-23 03:54:11

label 上的 for 属性必须引用 id。

<input type="number" id="fibo" name="fibo">
<label for="fibo">Enter the number to get a fibonacci</label>

A for attribute on label must by referencing to an id.

<input type="number" id="fibo" name="fibo">
<label for="fibo">Enter the number to get a fibonacci</label>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文