-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.html
227 lines (201 loc) · 7.58 KB
/
fibonacci.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="PRAGMA" content="NO-CACHE">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<meta http-equiv="EXPIRES" content="Sun, 7 Dec 1941 17:42:00 GMT">
<meta http-equiv="ROBOTS" content="NONE">
<meta http-equiv="GOOGLEBOT" content="NOARCHIVE">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta name="author" content="Jason Cust">
<style>
code {
font-family: monospace;
}
#fibMax {
min-width: 17em;
}
</style>
<script type="text/javascript">
( function ( win, doc, undef ) {
'use strict'; // Declaration for ECMA5 compliant JavaScript engines
var isEmpty = function ( obj ) {
// obj == undefined --> ( obj === undefined || obj === null )
if ( obj == undef || obj === '' ) {
return true;
}
},
genFibSequence = ( function () {
var getFibByIndex = ( function () {
var sq5 = Math.sqrt ( 5 ),
gm = ( 1 + sq5 ) / 2;
return function ( index ) {
return Math.round( Math.pow( gm, index ) / sq5 );
};
})();
return function( max ) {
var fibSeq = [],
fib;
for ( fib = 0; fib <= max; fib = getFibByIndex( fibSeq.length ) ) {
fibSeq.push( fib );
}
return fibSeq;
};
})(),
genOutputStr = function ( fibSeq, oFormat, docTitle, docDesc ) {
var serializer = new XMLSerializer(),
doc,
titleEl,
descEl,
seqEl,
numEl;
if ( oFormat.match( /^(ht|x)ml$/ ) ) {
if ( oFormat === 'html' ) {
doc = document.implementation.createHTMLDocument( docTitle );
titleEl = doc.createElement( 'h3' );
titleEl.textContent = docTitle;
doc.body.appendChild( titleEl );
descEl = doc.createElement( 'h5' );
descEl.textContent = docDesc;
doc.body.appendChild( descEl );
fibSeq.map( function( i ) {
numEl = doc.createElement( 'p' );
numEl.textContent = i;
doc.body.appendChild( numEl );
});
}
else {
doc = document.implementation.createDocument( 'http://www.w3.org/1999/xhtml', 'xml', null );
titleEl = doc.createElement( 'title' );
titleEl.textContent = docTitle;
doc.documentElement.appendChild( titleEl );
descEl = doc.createElement( 'description' );
descEl.textContent = docDesc;
doc.documentElement.appendChild( descEl );
seqEl = doc.createElement( 'sequence' );
doc.documentElement.appendChild( seqEl );
fibSeq.map( function( i ) {
numEl = doc.createElement( 'number' );
numEl.textContent = i;
seqEl.appendChild( numEl );
});
}
return serializer.serializeToString( doc );
}
else if ( oFormat === 'json' ) {
return JSON.stringify( { title: docTitle, description: docDesc, sequence: fibSeq }, null, '\t' );
}
else {
return fibSeq.join(', ');
}
},
init = function ( max ) {
var FIB_MIN = 1,
FIB_MAX = 31999,
inputError = 'Input max sequence value must be an integer between ' + FIB_MIN + ' and ' + FIB_MAX + '.';
if ( isEmpty( max ) ) {
return this._max;
}
else {
if ( max % 1 !== 0 || max < FIB_MIN || max > FIB_MAX ) {
throw new SyntaxError( inputError );
}
this._max = max;
this._sequence = genFibSequence( max );
return this;
}
},
valueByIndex = function ( index ) {
if ( !isEmpty( index ) ) {
return this._sequence[ index ];
}
},
firstValue = function () {
return this._sequence[ 0 ];
},
lastValue = function () {
return this._sequence[ this._sequence.length - 1 ];
},
sequence = function () {
return this._sequence;
},
fibSeqObj = function () { init.apply( this, arguments ); },
fibObj = function () { fibSeqObj.apply( this, arguments ); };
fibSeqObj.prototype = {
max: function () { return init.apply( this, arguments ); },
valueByIndex: function () { return valueByIndex.apply( this, arguments ); },
firstValue: function () { return firstValue.apply( this, arguments ); },
lastValue: function () { return lastValue.apply( this, arguments ); },
sequence: function () { return sequence.apply( this, arguments ); }
};
fibObj.prototype = Object.create( fibSeqObj.prototype );
fibObj.prototype.constructor = fibObj;
fibObj.prototype.display = function ( elID, format, docTitle, docDesc ) {
var matches = isEmpty( format ) || format.toString().toLowerCase().match( /^(html|xml|json|text)$/ );
var oDisp = isEmpty( elID ) || document.getElementById( elID.toString() );
if ( isEmpty( oDisp ) || isEmpty( matches ) ) {
throw new SyntaxError( 'The output HTML element ID and the format (HTML, XML, JSON, Text) must be specified.' );
}
oDisp.textContent = genOutputStr( this.sequence(), matches[ 1 ], docTitle, docDesc ) ;
};
fibObj.prototype.save = function ( type, docTitle, docDesc ) {
var matches = isEmpty( type ) || type.toString().toLowerCase().match( /^(html|xml|json|text)$/ );
if ( isEmpty( matches ) ) {
throw new SyntaxError( 'The output format (HTML, XML, JSON, Text) must be specified.' );
}
var uriContent = "data:application/octet-stream," + encodeURIComponent( genOutputStr( this.sequence(), matches[ 1 ], docTitle, docDesc ) );
window.open( uriContent, docTitle );
};
// Expose objects to win object
win[ 'FibonacciSequence' ] = fibSeqObj;
win[ 'Fibonacci' ] = fibObj;
})( window, document );
var getRadioVal = function ( el ) {
var i,
len = el.length;
for ( i = 0; i < len; i++ ) {
if ( el[ i ].checked) {
return el[ i ].value;
}
}
},
runFibSeq = function () {
var fibMax = document.fibSeq.fibMax.value,
oFormat = getRadioVal( document.fibSeq.outputFormat ),
oLocation = getRadioVal( document.fibSeq.outputLocation ),
oDisplay = 'outputDisplay',
docTitle = 'Fibonacci Sequence',
docDesc = 'This is a fibonacci sequence up to and including ' + fibMax + '.',
fib = new Fibonacci( fibMax );
if ( oLocation === 'file' ) {
fib.save( oFormat, docTitle, docDesc );
}
else {
fib.display( oDisplay, oFormat, docTitle, docDesc );
}
return false;
}
</script>
</head>
<body>
<form name="fibSeq" onsubmit="return runFibSeq()">
<fieldset>
<legend>Fibonacci Sequence</legend>
<label for="fibMax">Max:</label>
<input type="number" id="fibMax" name="fibMax" placeholder="Enter a value from 1 to 31999" min="1" max="31999" autofocus required><br>
<label>Output Format:</label>
<input type="radio" name="outputFormat" value="text" id="outputFormatT" checked><label for="outputFormatT">Text</label>
<input type="radio" name="outputFormat" value="html" id="outputFormatH"><label for="outputFormatH">HTML</label>
<input type="radio" name="outputFormat" value="xml" id="outputFormatX"><label for="outputFormatX">XML</label>
<input type="radio" name="outputFormat" value="json" id="outputFormatJ"><label for="outputFormatJ">JSON</label><br>
<label>Output Location:</label>
<input type="radio" name="outputLocation" value="screen" id="outputLocationS" checked><label for="outputFormatS">Screen</label>
<input type="radio" name="outputLocation" value="file" id="outputLocationF"><label for="outputFormatF">File</label><br>
<input type="submit" name="getFib" value="Get Sequence">
</fieldset>
</form>
<code id="outputDisplay"></code>
</body>
</html>