Problem Statement: I need to design a simple html form that takes a limit as input and displays Fibonacci series up to the input given.
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:
index.html
<!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>
script.js
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 = 0; 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;
}
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.