Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Take input a number from user
- var numberRegEx = /[+-]?([0-9]*[.])?[0-9]+/;
- var bool = true;
- var ahead = true;
- while (bool) {
- var userInput = Number(prompt('Enter number of terms to print Fibonacci Series...'));
- if (numberRegEx.test(userInput)) {
- bool = false;
- } else {
- window.alert('Number is not valid!!');
- bool = window.confirm('Do you want to enter again..');
- ahead = bool;
- }
- }
- if (ahead) {
- // Displaying number which is entered by user
- document.write('Entered number by you = <strong>' + userInput + '</strong><br><br>');
- // Displaying Fibonacci series
- var firstTerm = 0;
- var secondTerm = 1;
- var temp;
- for (var i = 0; i <= userInput; i++) {
- document.write(firstTerm); // here print the term of fibonacci series
- if (i < userInput) {
- document.write(', '); // here print "," (comma) if next term is exist otherwise not
- }
- temp = firstTerm; // here first term is stored in temporary variable
- firstTerm = secondTerm; // here second term stored in first term
- secondTerm = temp + firstTerm; // here add previous values of first and second term in second term
- }
- }
Add Comment
Please, Sign In to add comment