In: Computer Science
<script type = "text/javascript">
var first_number = prompt("Please enter the first number for our sequence calculation");
var second_number = prompt("Please enter the second number for our sequence calculation");
first_number = parseInt(first_number);
second_number = parseInt(second_number);
console.log(first_number);
console.log(second_number);
var previous_number = first_number;
var next_number = second_number;
var count = 10;
while(count>0)
{
count = count - 1;
var new_number = previous_number + next_number;
previous_number = next_number;
next_number = new_number;
console.log(next_number)
}
I need to use the fib function to print the same number sequence without using a loop in the function. How do I use the recursive function?
check out the solution.
------------------------------------------
CODE:
<html>
<head>
<script type = "text/javascript">
function fib(previous_number, next_number, count)
{
// base condition
if(count == 0)
return 0;
// calcuate new number and print it and recursive
call
var new_number = previous_number + next_number;
console.log(new_number)
fib(next_number, new_number, count-1)
}
var first_number = prompt("Please enter the first number for our
sequence calculation");
var second_number = prompt("Please enter the second number for our
sequence calculation");
first_number = parseInt(first_number);
second_number = parseInt(second_number);
console.log(first_number);
console.log(second_number);
var previous_number = first_number;
var next_number = second_number;
var count = 10;
// recursive function call
fib(previous_number, next_number, count);
</script>
</head>
<body></body>
</html>
--------------------------------------------------
-------------------------------
OUTPUT :
===================================================================