-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy path02-BasicCoffeeScript.coffee
More file actions
370 lines (306 loc) · 7.64 KB
/
02-BasicCoffeeScript.coffee
File metadata and controls
370 lines (306 loc) · 7.64 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
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
363
364
365
366
367
368
369
370
require './prelude'
# Number formats
show '--- Number formats ---'
show 144
show 144.toString 2
show 9.81
show 2.998e8
# Rounding errors
show '--- Rounding errors ---'
p = 1/3
show 6*p is 2
show p+p+p+p+p+p is 2
show 2-1e-15 < p+p+p+p+p+p < 2+1e-15
# Rounding errors accumulate in loops
i = 0
i++ for angle in [0...2*Math.PI] by 1/3*Math.PI
show i # Gives 7 iterations and not 6
# Arithmetic operators
show '--- Arithmetic operators ---'
show 100 + 4 * 11
show (100 + 4) * 11
show 115 * 4 - 4 + 88 / 2
show 314 % 100
show 10 % 3
show 144 % 12
# String types
show '--- String types ---'
show 'Patch my boat with chewing gum.'
show 'The programmer pondered: "0x2b or not 0x2b"'
show "Aha! It's 43 if I'm not a bit off"
show "2 + 2 is equal to #{2 + 2}"
show 'Imagine if this was a
very long line of text'
show '''First comes A
then comes B'''
show """ 1
+ 1
--- # " The next line confuses docco
#{1 + 1}"""
# Escape characters
show '--- Escape characters ---'
show 'This is the first line\nAnd this is the second'
show 'A newline character is written like \"\\n\".'
show 'con' + 'cat' + 'e' + 'nate'
# String description of a type
show '--- String description of a type ---'
show typeof 4.5
# Unary minus
show '--- Unary minus ---'
show -(10 - 2)
# Booleans
show '--- Booleans ---'
show 3 > 2
show 3 < 2
# Chained comparisons
show '--- Chained comparisons ---'
show 100 < 115 < 200
show 100 < 315 < 200
# String comparisons
show '--- String comparisons ---'
show 'Aardvark' < 'Zoroaster'
show 'Itchy' != 'Scratchy'
# Logical operators
show '--- Logical operators ---'
show true and false
show true or false
show !true
show not false
# Exercise 1
show '--- Exercise 1 ---'
show ((4 >= 6) || ('grass' != 'green')) &&
!(((12 * 2) == 144) && true)
show (4 >= 6 or 'grass' isnt 'green') and
not(12 * 2 is 144 and true)
show (false or true) and not(false and true)
show true and not false
show 'grass' != 'green'
show '--- End of Exercise ---'
# Useless program
show '--- Useless program ---'
1; !false
show 'Nothing was intentionally output'
# Variables
show '--- Variables ---'
caught = 5 * 5
show caught
show caught + 1
caught = 4 * 4
show caught
# Tentacles
show '--- Tentacles ---'
luigiDebt = 140
luigiDebt = luigiDebt - 35
show luigiDebt
# Environment
show '--- Environment ---'
# To show the environment, use: show global or show window
show 'Also, your hair is on fire.'
# Function invocation
show '--- Function invocation ---'
show Math.max 2, 4
show 100 + Math.max 7, 4
show Math.max(7, 4) + 100
show Math.max(7, 4 + 100)
show Math.max 7, 4 + 100
# Explore the environment - Try this in the REPL
# show process
# show console
# show _
# show show
# Questions
show '--- Questions ---'
# chain is required here to wait for the answer
confirm 'Shall we, then?', (answer) -> show answer; chain1()
chain1 = ->
prompt 'Tell us everything you know.', '...',
(answer) -> show 'So you know: ' + answer; chain2()
chain2 = ->
prompt 'Pick a number', '', (answer) ->
theNumber = Number answer
show 'Your number is the square root of ' +
(theNumber * theNumber)
chain3()
chain3 = ->
# While loops
show '--- While loops ---'
show 0
show 2
show 4
show 6
show 8
show 10
show 12
currentNumber = 0
while currentNumber <= 12
show currentNumber
currentNumber = currentNumber + 2
counter = 0
while counter <= 12 then counter = counter + 2
# Exercise 2
show '--- Exercise 2 ---'
result = 1
counter = 0
while counter < 10
result = result * 2
counter = counter + 1
show result
show '--- End of Exercise ---'
# Exercise 3
show '--- Exercise 3 ---'
line = ''
counter = 0
while counter < 10
line = line + '#'
show line
counter = counter + 1
show '--- End of Exercise ---'
# Sneak peek at Functional solutions
show '--- Sneak peek at Functional solutions ---'
show _.reduce [1..10], ((x) -> 2*x), 1
_.reduce [1..10], ((s) -> show s += '#'), ''
# For loops
show '--- For loops ---'
show 'For on one line'
for number in [0..12] by 2 then show number
show 'For with indented body'
for number in [0..12] by 2
show number
show 'For with prepended body'
show number for number in [0..12] by 2
show 'For collecting results'
numbers = (number for number in [0..12] by 2)
show numbers
# Comments
show '--- Comments ---'
# The variable counter, which is about to be defined,
# is going to start with a value of 0, which is zero.
counter = 0
# Now, we are going to loop, hold on to your hat.
while counter < 100 # counter is less than one hundred
###
Every time we loop, we INCREMENT the value of counter
Seriously, we just add one to it.
###
counter++
# And then, we are done.
# Exercise 4
show '--- Exercise 4 ---'
result = 1
for counter in [0...10]
result = result * 2
show result
line = ''
for counter in [0...10]
line = line + '#'
show line
show '--- End of Exercise ---'
# Conditionals
show '--- Conditionals ---'
for counter in [0..20]
if counter % 3 == 0 and counter % 4 == 0
show counter
for counter in [0..20]
if counter % 4 == 0
show counter
if counter % 4 != 0
show '(' + counter + ')'
for counter in [0..20]
if counter % 4 == 0
show counter
else
show '(' + counter + ')'
for counter in [0..20]
if counter > 15
show counter + '**'
else if counter > 10
show counter + '*'
else
show counter
# Exercise 5
show '--- Exercise 5 ---'
prompt 'You! What is the value of 2 + 2?', '',
(answer) ->
if answer == '4'
show 'You must be a genius or something.'
else if answer == '3' || answer == '5'
show 'Almost!'
else
show 'You are an embarrassment.'
chain4()
show '--- End of Exercise ---'
chain4 = ->
# If variation
show '--- If variation ---'
fun = on
show 'The show is on!' unless fun is off
# Loop variations
show '--- Loop variations ---'
current = 20
loop
if current % 7 == 0
break
current++
show current
current = 20
current++ until current % 7 == 0
show current
# Exercise 6
show '--- Exercise 6 ---'
luckyNumber = 5 # Choose from 1 to 6
show "Your lucky number is #{luckyNumber}"
count = 0
loop
show roll = Math.floor Math.random() * 6 + 1
count++
if roll is luckyNumber then break
show "Luck took #{count} roll(s)"
luckyNumber = 3 # Choose from 1 to 6
show 'Your lucky number is ' + luckyNumber
count = 0
until roll is luckyNumber
show roll = Math.floor Math.random() * 6 + 1
count++
show 'You are lucky ' +
Math.floor(100/count) + '% of the time'
show '--- End of Exercise ---'
# Undefined variable
show '--- Undefined variable ---'
show mysteryVariable
mysteryVariable = 'nothing'
show console.log 'I am a side effect.'
# Existential operator
show '--- Existential operator ---'
show iam ? undefined
iam ?= 'I want to be'
show iam
iam ?= 'I am already'
show iam if iam?
# Type conversions
show '--- Type conversions ---'
show false == 0
show '' == 0
show '5' == 5
# String type conversions
show '--- String type conversions ---'
show 'Apollo' + 5
show null + 'ify'
show '5' * 5
show 'strawberry' * 5
show Number('5') * 5
# NaN
show '--- NaN ---'
show NaN == NaN
# Boolean type conversions
show '--- Boolean type conversions ---'
prompt 'What is your name?', '',
(input) ->
show 'Well hello ' + (input || 'dear')
chain5()
chain5 = ->
# Short circuit operators
show '--- Short circuit operators ---'
false || show 'I am happening!'
true || show 'Not me.'
# Exit from the chain of inputs
process.exit()