File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // for n number of fibonacci numbers
2+ const fibonacci = function ( n ) {
3+ if ( n == 1 ) return [ 1 ] ;
4+ else if ( n == 2 ) return [ 1 , 1 ] ;
5+
6+ let fib = [ 1 , 1 ] ;
7+ for ( i = 2 ; i < n ; i ++ ) {
8+ let temp_num = fib [ fib . length - 1 ] + fib [ fib . length - 2 ] ;
9+ fib . push ( temp_num ) ;
10+ }
11+
12+ return fib ;
13+ }
14+
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < title > Javascrip Algorithm</ title >
5+
6+ < script type ="text/javascript " src ="./fibonacci.js "> </ script >
7+ </ head >
8+ < body >
9+ < h1 > Go to console</ h1 >
10+ < br >
11+ < h2 > Type fibonacci(n), where n is the number of elements you want in series</ h2 >
12+
13+ < br >
14+ < br >
15+ < br >
16+
17+ < h3 > OR just playaround with number</ h3 >
18+
19+ < input id ="number " type ="number " placeholder ="input number " onchange ="displayFibs() ">
20+
21+ < div id ="fib_content "> </ div >
22+ </ body >
23+
24+ < script type ="text/javascript ">
25+ function displayFibs ( ) {
26+ const input = document . getElementById ( "number" )
27+ const fibs = fibonacci ( parseInt ( input . value ) )
28+ const content = document . getElementById ( "fib_content" )
29+ content . innerHTML = ""
30+
31+ for ( i = 0 ; i < fibs . length ; i ++ ) {
32+ let div = document . createElement ( 'span' ) ;
33+
34+ div . innerHTML = "<span>" + fibs [ i ] + ", </span>"
35+ content . appendChild ( div ) ;
36+ }
37+ }
38+ </ script >
39+ </ html >
You can’t perform that action at this time.
0 commit comments