Question

In: Computer Science

<script type = "text/javascript">     var  first_number = prompt("Please enter the first number for our sequence calculation");     var  second_number...

<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?

Solutions

Expert Solution

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 :

===================================================================


Related Solutions

Write a PowerShell script which will prompt user to enter the number of the day of...
Write a PowerShell script which will prompt user to enter the number of the day of the week (e.g. 1,2,3,4,5,6,7) and return the day of the week. (e.g. Sunday...etc.) (Hint: Sunday is the 1st day of the week).
5) Write a JavaScript script that will ask the user to enter in the length of...
5) Write a JavaScript script that will ask the user to enter in the length of the side of a cube. (all the sides of a cube are equal) Based on the value that is entered by the user from the keyboard, calculate each of the following and assign your results to variables. 1. volume of the cube 2. surface area of the cube 3. volume of the largest sphere that could fit in the box. 4. surface area of...
In C++ Prompt user to enter two integers •Determine whether the first number is divisible by...
In C++ Prompt user to enter two integers •Determine whether the first number is divisible by the second. If the second number is zero, the program should not do division •Output the remainder of the two numbers •Compare the two integers, display the integers in non-decreasing order.
in java Create simple number guessing game as follows. First, prompt the user to enter a...
in java Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with...
Prompt the user to enter a string of their choosing. Store the text in a string....
Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and,...
Can someone verify why my code isnt running <!DOCTYPE html> <html> <body bgcolor=aqua text=purple> <script type="text/javascript">...
Can someone verify why my code isnt running <!DOCTYPE html> <html> <body bgcolor=aqua text=purple> <script type="text/javascript"> funtion oldMacVerse(animal, sound) //Assumes: animal is the name of an animal, sound is the sound it makes //Results: displays a version of the song "Old MacDonals Had a Farm" in outputDiv { document.getElementById('outputDiv').innerHTML =    '<p>Old MacDonald had a farm, E-I-E-I-O.<br>' +    'And on that farm he had a ' + animal + ', E-I-E-I-O.<br>' +    'With a ' + sound +...
<!DOCTYPE html> <html lang="en"> <head>    <title>Form Display Example</title>    <script type="text/javascript">    function display() {...
<!DOCTYPE html> <html lang="en"> <head>    <title>Form Display Example</title>    <script type="text/javascript">    function display() {    dispWin = window.open('','NewWin','toolbar=no,status=no,width=300,height=200')       message = "<ul><li>NAME:" + document.form1.name.value;    message += "<li>ADDRESS:" + document.form1.address.value;    message += "<li>PHONE:" + document.form1.phone.value;    message += "</ul>";    dispWin.document.write(message); } </script> </head> <body>    <h1>Form Display Example</h1>    <p>Enter the following information. When you press the Display button, the data you entered will be displayed in a pop-up.</p>    <form name="form1" method="get" action="">   ...
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT