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...
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)....
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.
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) 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...
“1. What determines blood type? “ Click here to enter text. “2. What type of blood...
“1. What determines blood type? “ Click here to enter text. “2. What type of blood antigens are expressed if a person is blood type AB negative? “ Click here to enter text. “3. Why doesn’t a transfusion reaction occur the first time an Rh negative patient is exposed to Rh positive blood? “ Click here to enter text. What surprised you about the anatomy of the sheep’s heart? Click here to enter text. Research diseases of the heart valves....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT