forked from sunnychen90/TheJavaScriptEncyclopedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR.html
362 lines (309 loc) · 14.8 KB
/
R.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<html>
<head>
<title>The JavaScript Encyclopedia: R</title>
<link rel="stylesheet" href="encyclopedia.css" type="text/css">
</head>
<body><h1>R</h1>
<h2 id="radians">radians</h2>
<p>Blah.</p>
<pre>function degrees(radians) {
return radians * 180 / Math.PI;
}
function radians(degrees) {
return radians * Math.PI / 180;
}</pre>
<h2>raise</h2>
<h3>raise exception</h3>
<p>See <a href="T.html#throw statement"><code>throw</code> statement</a>.</p>
<h3>raise to a power</h3>
<p>See <a href="P.html#pow Math function"><code>pow</code> <code>Math</code> function <dfn>raise to a power</dfn></a>.</p>
<h2 id="random">random</h2>
<h3 id="random Math function"><code>random</code> <code>Math</code> function</h3>
<p>The <code>Math.random()</code> function returns a number that is greater than or equal to 0 and less than 1. Each time <code>Math.random()</code>is called it is likely to produce a different result. Unfortunately, it is not guaranteed to have sufficient randomness for cryptographic or gambling applications.</p>
<p>Because <code>Math.random()</code> produces a number between 0 and 1, it is usually necessary to scale it a to desired range. For example, to simulate the rolling for a six sided die, use <code>Math.floor(Math.random()*</code> <code>6)</code> <code>+</code> <code>1</code>.</p>
<p>Flipping a coin is simpler:</p>
<pre>cointoss = Math.random() >= 0.5; // true is heads, false is tails</pre>
<p>An <a href="A.html#array">array</a> representing a deck of 52 cards can be shuffled with 51 random numbers.</p>
<pre>function shuffle(deck) {
// Shuffle the contents of an array using the Fisher-Yates shuffle. Starting
// at the end of the deck, swap each card with a card randomly selected from
// the front part of the deck.
var card_nr = deck.length,
rand, // The index of the randomly selected card
temp; // Temporary storage for the swap.
while (card > 1) {
rand = Math.floor(Math.random() * card_nr);
card_nr -= 1;
temp = deck[card_nr];
deck[card_nr] = deck[rand];
deck[rand] = temp;
}
}
</pre>
<h2 id="RangeError">RangeError</h2>
<h3 id="RangeError global function"><code>RangeError</code> global function</h3>
<p>Blah.</p>
<h2 id="recursion">recursion</h2>
<p>Blah.</p>
<pre>function solve(data, name) {
// The solve function takes a network object and the name of a node in that
// network, and finds the lowest cost path to a terminal node.
// There are two kinds of nodes. An ordinary node contains up and down
// properties which describe the local cost of the next transition. A terminal
// node contains a cost property, and not up or down properties.
// The solve function depends on two recursive functions, chase and follow,
// to do its work.
var results = [], // An array of strings, the names on the cheapest path.
solution = {}; // The cheapest path from this node and the total cost
// from this node.
function chase(name) {
// Given the name of a node in the data set, compute the least total cost from
// this node to a terminal node. Find the solutions to the up and down nodes,
// and make a solution that is the cheaper for this node. This function finds
// the up and down solutions by calling itself.
var down,
down_cost,
down_solution,
lemma = solution[name],
node,
up,
up_cost,
up_solution;
// If there is already a solution for this node,
// then there is nothing to be done.
if (!lemma) {
node = data[name];
up = node.up;
down = node.down;
// If the node has an up, find the up solution.
if (up) {
up_solution = chase(up.next);
up_cost = up.cost + up_solution.cost;
// And if the node also has a down, find the down solution.
if (down) {
down_solution = chase(down.next);
down_cost = down.cost + down_solution.cost;
// Choose the less expensive solution.
if (up.cost < down.cost) {
lemma = {next: up.next, cost: up_cost};
} else {
lemma = {next: down.next, cost: down_cost};
}
// If there wasn't a down, choose the up solution.
} else {
lemma = {next: up.next, cost: up_cost};
}
} else {
// If there wasn't an up, choose the down solution.
if (down) {
down_solution = chase(down.next);
down_cost = down.cost + down_solution.cost;
lemma = {next: down.next, cost: down_cost};
// If this was a terminal node, then take the node.
} else {
lemma = node;
}
}
// Record this node in the solution set.
solution[name] = lemma;
}
// Return the solution for this node.
return lemma;
}
function follow(name) {
// Make a list of the names that make the cheapest path.
// This function obtains the succeeding names by calling
// itself.
if (name) {
results.push(name);
follow(solution[name].next);
}
}
// Now we can call chase and follow and return the results.
chase(name);
follow(name);
return results;
}
var data = {
a: {
up: {next: 'c', cost: 1},
down: {next: 'd', cost: 0}
},
c: {
up: {next: 'e', cost: 5},
down: {next: 'f', cost: 4}
},
d: {
up: {next: 'f', cost: 7},
down: {next: 'g', cost: 3}
},
e: {
up: {next: 'h', cost: 2},
down: {next: 'i', cost: 1}
},
f: {
up: {next: 'i', cost: 1},
down: {next: 'j', cost: 2}
},
g: {
up: {next: 'j', cost: 5},
down: {next: 'k', cost: 4}
},
h: {
down: {next: 'l', cost: 3}
},
i: {
up: {next: 'l', cost: 3},
down: {next: 'm', cost: 4}
},
j: {
up: {next: 'm', cost: 2},
down: {next: 'n', cost: 2}
},
k: {
up: {next: 'n', cost: 2}
},
l: {
down: {next: 'o', cost: 5}
},
m: {
up: {next: 'o', cost: 2},
down: {next: 'p', cost: 8}
},
n: {
up: {next: 'p', cost: 4}
},
o: {
down: {next: 'b', cost: 2}
},
p: {
up: {next: 'b', cost: 1}
},
b: {cost: 0}
};
alert(solve(data, 'a'));
</pre>
<h2 id="reduce">reduce</h2>
<h3 id="reduce Array prototype function"><code>reduce</code> <code>Array</code> prototype function</h3>
<p>Blah.</p>
<h2 id="reduceRight">reduceRight</h2>
<h3 id="reduceRight Array prototype function"><code>reduceRight</code> <code>Array</code> prototype function</h3>
<p>Blah.</p>
<h2 id="reference">reference</h2>
<p>Blah.</p>
<h2 id="ReferenceError">ReferenceError</h2>
<h3 id="ReferenceError global function"><code>ReferenceError</code> global function</h3>
<p>Blah.</p>
<h2 id="regexp">regexp</h2>
<p>Blah.</p>
<h3 id="RegExp global function"><code>RegExp</code> global function</h3>
<p>Blah.</p>
<h3 id="RegExp prototype function"><code>RegExp</code> prototype function</h3>
<p>Blah.</p>
<h3 id="regexp literal">regexp literal</h3>
<p>Blah. 7.8.5</p>
<h3 id="regexp operator">regexp operator</h3>
<p>Blah.</p>
<h2 id="regular expression">regular expression</h2>
<p>See <a href="#regexp">regexp</a>.</p>
<h2 id="relational operator">relational operator</h2>
<p>The relational operators are</p>
<ul>
<li><a href="bang= infix operator"><code>!=</code> infix operator</a> <dfn>coercing
not equal</dfn></li>
<li><a href="bang== infix operator"><code>!==</code> infix operator</a> <dfn>not equal</dfn></li>
<li><a href="< infix operator"><code><</code> infix operator</a> <dfn>less than</dfn></li>
<li><a href="<= infix operator"><code><=</code> infix operator</a> <dfn>less than or equal</dfn></li>
<li><a href="== infix operator"><code>==</code> infix operator</a> <dfn>coercing equal</dfn></li>
<li><a href="=== infix operator"><code>===</code> infix operator</a> <dfn>equal</dfn></li>
<li><a href="> infix operator"><code>></code> infix operator</a> <dfn>greater than</dfn></li>
<li><a href=">= infix operator"><code>>=</code> infix operator</a> <dfn>greater than or equal</dfn></li>
<li><a href="in infix operator"><code>in</code> infix operator</a></li>
<li><a href="instanceof infix operator"><code>instanceof</code> infix operator</a></li>
</ul>
<p>Every relational operator is an <a href="I.html#infix operator">infix operator</a>. They all return either the <a href="T.html#true">true</a> value or the <a href="F.html#false">false</a> value depending on the relationship between the two operands. They resemble mathematical symbols but do not compose like mathematical symbols. The expression</p>
<pre>a < b < c</pre>
<p>looks like it might be <a href="T.html#true">true</a> if <code>a</code> is less than <code>b</code> and if <code>b</code> is also less than <code>c</code>, but that is not what it means. The expression first compares <code>a</code> and <code>b</code>, producing either <a href="T.html#true">true</a> or <a href="F.html#false">false</a>. Then <code>c</code> will be compared to either <a href="T.html#true">true</a> or <a href="F.html#false">false</a>, not to <code>b</code>. </p>
<h2 id="replace">remainder</h2>
<p>See <a href="special.html#% infix operator"><code>%</code> infix operator</a> <dfn>remainder</dfn>.</p>
<h2>replace</h2>
<h3 id="replace String prototype function"><code>replace</code> <code>String</code> prototype function</h3>
<p>Blah.</p>
<h2 id="requestor">requestor</h2>
<p>Blah.</p>
<h2 id="reserved word">reserved word</h2>
<p>A <strong>reserved word</strong> is an <a href="N.html#name">identifier</a> that is reserved for the exclusive use of the language itself. A reserved word may be used as a <a href="S.html#statement">statement</a>, <a href="O.html#operator">operator</a>, or a future feature of tomorrow. The reserved words are</p>
<ul><li><a href="B.html#break"><code>break</code></a></li>
<li><a href="C.html#case"><code>case</code></a></li>
<li><a href="C.html#catch"><code>catch</code></a></li>
<li><a href="C.html#const"><code>const</code></a></li>
<li><a href="C.html#continue"><code>continue</code></a></li>
<li><a href="D.html#debugger"><code>debugger</code></a></li>
<li><a href="D.html#default"><code>default</code></a></li>
<li><a href="D.html#delete"><code>delete</code></a></li>
<li><a href="D.html#do"><code>do</code></a></li>
<li><a href="E.html#else"><code>else</code></a></li>
<li><a href="E.html#enum"><code>enum</code></a></li>
<li><a href="E.html#export"><code>export</code></a></li>
<li><a href="E.html#extends"><code>extends</code></a></li>
<li><a href="F.html#finally"><code>finally</code></a></li>
<li><a href="F.html#for"><code>for</code></a></li>
<li><a href="F.html#finally"><code>function</code></a></li>
<li><a href="I.html#if"><code>if</code></a></li>
<li><a href="I.html#implements"><code>implements</code></a></li>
<li><a href="I.html#import"><code>import</code></a></li>
<li><a href="I.html#in"><code>in</code></a></li>
<li><a href="I.html#instanceof"><code>instanceof</code></a></li>
<li><a href="I.html#interface"><code>interface</code></a></li>
<li><a href="L.html#let"><code>let</code></a></li>
<li><a href="N.html#new"><code>new</code></a></li>
<li><a href="P.html#package"><code>package</code></a></li>
<li><a href="P.html#private"><code>private</code></a></li>
<li><a href="P.html#protected"><code>protected</code></a></li>
<li><a href="P.html#public"><code>public</code></a></li>
<li><a href="R.html#return"><code>return</code></a></li>
<li><a href="S.html#static"><code>static</code></a></li>
<li><a href="S.html#super"><code>super</code></a></li>
<li><a href="S.html#switch"><code>switch</code></a></li>
<li><a href="T.html#this"><code>this</code></a></li>
<li><a href="T.html#throw"><code>throw</code></a></li>
<li><a href="T.html#try"><code>try</code></a></li>
<li><a href="T.html#typeof"><code>typeof</code></a></li>
<li><a href="V.html#var"><code>var</code></a></li>
<li><a href="V.html#void"><code>void</code></a></li>
<li><a href="W.html#while"><code>while</code></a></li>
<li><a href="W.html#with"><code>with</code></a></li>
<li><a href="Y.html#yield"><code>yield</code></a></li>
</ul>
<p>A reserved word may not be used as the name of a <a href="F.html#function">function</a>, <a href="V.html#variable">variable</a>, <a href="P.html#parameter">parameter</a>, or <a href="L.html#label">label</a>. Some of the reserved words are not actually used in any edition of the language. </p>
<pre>var break; // syntax error
var breaky; // ok</pre>
<p>The constants <a href="F.html#false"><code>false</code></a>, <a href="N.html#null"><code>null</code></a>, and <a href="T.html#true"><code>true</code></a> are not formally reserved words, but they have the same restrictions. The constants <a href="I.html#Infinity"><code>Infinity</code></a>, <a href="N.html#NaN"><code>NaN</code></a>, and <a href="U.html#undefined"><code>undefined</code></a> do not have these restrictions.</p>
<pre>var null; // syntax error
var undefined; // ok</pre>
<div class="es3">
<p> ES3 places additional restrictions on the use of reserved words. A reserved word may not be used with the <a href="special.html#period suffix operator"><code>.</code> suffix operator</a> <dfn>select</dfn>. You can instead use the <a href="special.html#leftbracket] suffix operator"><code>[]</code> suffix operator</a> <dfn>subscript</dfn> with a <a href="S.html#string literal">string literal</a>. So</p>
<pre>my_object.break // ES3 syntax error</pre>
<p>is not allowed, but</p>
<pre>my_object['break']</pre>
<p>is allowed. Similarly, a reserved word cannot be used in the key position of an <a href="O.html#object literal">object literal</a> but is allowed if it is quoted as a <a href="S.html#string literal">string literal</a>. So</p>
<pre>{break: true} // ES3 syntax error</pre>
<p>is not allowed, but</p>
<pre>{"break": true}</pre>
<p>is allowed.</p></div>
<h2>return <a href="R.html#reserved word"><strong>reserved word</strong></a></h2>
<h3 id="return statement"><code>return</code> statement</h3>
<p>Blah.</p>
<h2 id="reverse">reverse</h2>
<h3 id="reverse Array prototype function"><code>reverse</code> <code>Array</code> prototype function</h3>
<p>Blah.</p>
<h2 id="round">round</h2>
<h3 id="round Math function"><code>round</code> <code>Math</code> function</h3>
<p>The <code>Math.round(</code><var>number</var><code>)</code> function takes a number argument and returns the nearest integer. It could be implemented as</p>
<pre>Math.number = function (number) {
var value = +number;
return (value < 0 && value >= -0.5) || (value === 0 && 1 / value < 0) ? -0 :
Math.floor(+value + 0.5);
};</pre>
</body>
</html>