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).
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 +...
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...
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.
4. Prompt user to enter a double number from console. Round this number so it will...
4. Prompt user to enter a double number from console. Round this number so it will keep 2 places after the decimal point. Display new number. For example, number 12.3579 will round to 12.36; number 12.4321 will round to 12.43 This is meant to be written in Java 14.0
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line)....
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) 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...
Prompt user to enter an integer number from console. Use 3 methods to check if this...
Prompt user to enter an integer number from console. Use 3 methods to check if this number is even or odd. Display result. This is required to be done in MIPS assembly language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT