-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheat-sheet.html
More file actions
182 lines (174 loc) · 4.66 KB
/
cheat-sheet.html
File metadata and controls
182 lines (174 loc) · 4.66 KB
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
<!DOCTYPE html>
<head>
<title>Cheatsheet</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="newcss.css"/>
</head>
<body style="background-image:url(http://www.womenshealthmag.com/files/wh6_uploads/images/froot-loops.jpg)">
<div id="container">
<h1>Looping in Ruby vs looping in Javascript</h1>
<div class="loop">
<h2>Looping with 'while'</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
while /condition/ do
.........
end
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
while (/condition/) {
...........
}
</pre>
</div>
</div>
<div class="loop">
<h2>Looping certain amount of times</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
n.times do
..........
end
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
for ( var n=x; n /condition/; n++) {
.........
}
</pre>
</div>
</div>
<div class="loop">
<h2>Iterating through elements of array</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
array.each do
..........
end
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
var a = array;
for (var i=0; i < a.length; i++) {
.......................
}
</pre>
</div>
</div>
<div class="loop">
<h2>breaking out of loop</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
break
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
break
</pre>
</div>
</div>
<div class="loop">
<h2>Each method</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
array.each {|element| code}
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
var each = function(array,func) {
for (var i=0; i < array.length; i++) {
func(array[i]);
}
};
var bugs = [/array/];
each(bugs,function(x) {console.log(x);});
</pre>
</div>
</div>
<div class="loop">
<h2>'map' method</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
array.map {|element| code}
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
var results =[];
for (var i=0; i < array.length; i++) {
results.push(func(array[i]));
}
return results;
};
var nums = [5,12,2,32,54];
var timesTwo=function(x){return x*2};
var numsTimesTwo = map(nums,timesTwo);
</pre>
</div>
</div>
<div class="loop">
<h2>'select' method</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
array.select {|element| code}
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
var select = function(array,func) {
var results =[];
for (var i=0; i < array.length; i++) {
if (func(array[i])) {
results.push(func(array[i]));
}
}
return results;
};
</pre>
</div>
</div>
<div class="loop">
<h2>'max' method</h2>
<div class="left">
<h4>Ruby</h4>
<pre>
array.max
</pre>
</div>
<div class="right">
<h4>Javascript</h4>
<pre>
var max = function(array) {
var biggest =array[0];
for (var i=0; i < array.length; i++) {
if (array[i] > biggest) {
biggest = array[i]
}
}
return biggest;
};
</pre>
</div>
</div>
</div>
</body></html>