-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrosetta.html
More file actions
247 lines (162 loc) · 5.35 KB
/
rosetta.html
File metadata and controls
247 lines (162 loc) · 5.35 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
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
<script>
// Rule of thumb: put a semicolon at the end of every line that doesn't end in a curly
// Simple output
// =============
// puts "howdy!"
// alert("howdy!");
// Declaring local variables
// =========================
// name = "Raghu"
// puts "howdy, #{name}!"
// var name = "Raghu";
// alert("howdy, " + name + "!");
// Note: there's no String interpolation in JS
// Defining and calling methods
// ============================
// def greet
// puts "howdy!"
// end
// greet
// function greet() {
// alert("howdy!");
// }
// greet();
// Methods that accept arguments
// =============================
// def greet(name)
// puts "howdy, #{name}!"
// end
// function greet(name) {
// alert("howdy, " + name + "!");
// }
// greet("Raghu");
// Conditionals
// ============
// city = "Toledo"
// if city == "Chicago"
// puts "You live in the best city ever!"
// else
// puts "You live in a so-so city."
// end
// var city = "Toledo";
// if (city === "Chicago") {
// alert("You live in the best city ever!");
// }
// else {
// alert("You live in a so-so city.");
// }
// Beware: there's also a == (non-strict equivalence) operator. Look it up.
// Incrementing integers
// =====================
// i += 1
// i++
// For loops
// =========
// for i in 2...6
// puts i * i
// end
// for (var i = 2; i < 6; i++) {
// alert(i * i);
// }
// Methods that accept blocks of code
// ==================================
// def announce(&instructions)
// puts "********** DEAR PASSENGER **********"
// instructions.call
// puts "********** THANK YOU FOR FLYING ONE-WAY AIR **********"
// end
// announce do
// puts "Your flight is boarding in 5 minutes."
// end
// function announce(instructions) {
// alert("********** DEAR PASSENGER **********");
// instructions();
// alert("********** THANK YOU FOR FLYING ONE-WAY AIR **********");
// }
// announce(function() {
// alert("Your flight is boarding in 5 minutes.");
// });
// Methods that accept arguments and blocks
// ========================================
// def announce(salutation, valediction, &instructions)
// puts "********** #{salutation} **********"
// instructions.call
// puts "********** #{valediction} **********"
// end
// announce("DEAR PASSENGER", "THANK YOU FOR FLYING ONE-WAY AIR") do
// puts "Your flight is boarding in 5 minutes."
// end
// function announce(salutation, valediction, instructions) {
// alert("********** " + salutation + " **********");
// instructions();
// alert("********** " + valediction + " **********");
// }
// announce("DEAR PASSENGER", "THANK YOU FOR FLYING ONE-WAY AIR", function() {
// alert("Your flight is boarding in 5 minutes.");
// });
// Providing block variables
// =========================
// def announce(&instructions)
// puts "********** DEAR PASSENGER **********"
// instructions.call("*")
// puts "********** THANK YOU FOR FLYING ONE-WAY AIR **********"
// end
// announce do |special_character|
// puts "#{special_character} Your flight is boarding in 5 minutes. #{special_character}"
// end
// function announce(instructions) {
// alert("********** DEAR PASSENGER **********");
// instructions("*");
// alert("********** THANK YOU FOR FLYING ONE-WAY AIR **********");
// }
// announce(function(specialCharacter) {
// alert(specialCharacter + " Your flight is boarding in 5 minutes. " + specialCharacter);
// });
// Methods with arguments, blocks, and block variables
// ===================================================
// def announce(salutation, valediction, &instructions)
// puts "********** #{salutation} **********"
// instructions.call("*")
// puts "********** #{valediction} **********"
// end
// announce("DEAR PASSENGER", "THANK YOU FOR FLYING ONE-WAY AIR") do |special_character|
// puts "#{special_character} Your flight is boarding in 5 minutes. #{special_character}"
// end
// function announce(salutation, valediction, instructions) {
// alert("********** " + salutation + " **********");
// instructions("*");
// alert("********** " + valediction + " **********");
// }
// announce("DEAR PASSENGER", "THANK YOU FOR FLYING ONE-WAY AIR", function(specialCharacter) {
// alert(specialCharacter + " Your flight is boarding in 5 minutes. " + specialCharacter);
// });
// Arrays
// ======
// fruits = ["apples", "oranges", "bananas"]
// puts fruits[1]
// puts fruits.length
// var fruits = ["apples", "oranges", "bananas"];
// alert(fruits[1]);
// alert(fruits.length);
// CHALLENGE
// =========
// Translate the following Ruby into Javascript.
// def for_each_in(array, &instructions)
function for_each_in(array, instructions) {
// for i in 0...array.length
for (var i = 0; i < array.length; i++) {
// instructions.call(array[i])
instructions(array[i]);
// end
}
// end
}
// fruits = ["apples", "oranges", "bananas", "pears"]
var fruits = ["apples", "oranges", "bananas", "pears"];
// for_each_in(fruits) do |f|
for_each_in(fruits, function(f) {
// puts "I like #{f}."
alert("I like " + f + ".");
// end
});
</script>