-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenges.yml
1406 lines (1327 loc) · 56.8 KB
/
challenges.yml
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Episode 1
# LetsWritePhp
letswritephp_openphpmc:
question: |
Everything in a PHP file is just HTML code at first. If you want
to write some dynamic PHP code, you'll activate PHP mode by always
writing this first:
explanation: |
As soon as you write `<?php` you are in PHP mode. So, `<?php echo 'Hello'; ?>`
gets you into PHP mode and executes `echo 'Hello'`. The `?>` gets
you *out* of PHP mode, and back into normal HTML land.
features:
multiple_choice:
choices:
a: <PHP
b: <?php
c: ?>
d: echo
correct: b
letswritephp_simpleechocoding:
question: |
AirPupNMeow needs you to create their site! You'll do it for free,
because you need the exposure.
question_steps:
- 'Start by opening PHP, then echo their tag line: "I luv puppies" inside an `<h2>` tag.'
step: lets-write-php-simple-echo
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_echo:
type: php
assertTrue: inputContains('index.php', 'echo')
failure: I don't see `echo` in your file
check_output:
type: http
assertTrue: body() matches '/I luv puppies/'
failure: I don't see `I luv puppies` anywhere - did you print this?
check_h2:
type: http
assertTrue: css("h2:contains('I luv puppies')").count() > 0
failure: Make sure the `I luv puppies` text is being printed *inside* an `h2` tag
letswritephp_jobofechomc:
question: |
What's the job of the `echo` statement?
explanation: |
`echo` - as in `echo 'this question was no problem';` - is used to print anything
out. There are a few other ways to print stuff, but `echo` is by far the most important.
You rock `echo`!
features:
multiple_choice:
choices:
a: to print things out
b: to exit HTML mode and enter PHP mode
c: to end the file
d: to create a variable
correct: a
letswritephp_createvariablecoding:
question: |
The management of AirPupNMeow is always changing its mind. To simplify
the life of the dev team, let's use a variable so that when management
changes the tag line, we only have to update one spot.
question_steps:
- Create a variable called `airpupTag`and set it to the `I luv puppies` text.
- Now, print this inside the `<h2>` tag
step: lets-write-php-create-variable
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
check_variable:
type: php
assertTrue: variable('airpupTag') === 'I luv puppies'
failure: Make sure the `$airpupTag` variable is set to `I luv puppies`
look_for_echo:
type: php
assertTrue: inputContains('index.php', 'echo')
failure: I don't see `echo` in your file
check_output:
type: http
assertTrue: body() matches '/I luv puppies/'
failure: I don't see `I luv puppies` anywhere - did you print this?
check_h2:
type: http
assertTrue: css("h2:contains('I luv puppies')").count() > 0
failure: Make sure the `I luv puppies` text is being printed *inside* an `h2` tag
letswritephp_fixmissingsemicoloncoding:
question: |
Poor devs. Management was so excited about variables that they
tried to edit the code themselves. We've sent the dev team for
ice cream to make up for it. While they're gone...
question_steps:
- Fix the errors in this file for them
step: lets-write-php-fix-missing-semicolon
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
check_output:
type: http
assertTrue: body() matches '/I luv puppies/'
failure: I don't see `I luv puppies` anywhere - did you print this?
check_h2:
type: http
assertTrue: css("h2:contains('I luv puppies')").count() > 0
failure: Make sure the `I luv puppies` text is being printed *inside* an `h2` tag
check_h2_2015:
type: http
assertTrue: css("h2:contains('2015')").count() > 0
failure: Make sure the `2015` text is being printed *inside* an `h2` tag
# Functions
functions_userandfunctioncoding:
question: |
It's hard to keep track of how many loveable pets we have
at a time. To save time (read: make management happy) let's just
print a random number of pets.
question_steps:
- Print a random number - between 100 and 200 - for the number of tail-wagging pets we have available.
step: functions-use-rand-function
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
check_output:
type: http
assertTrue: body() matches '/I luv puppies/'
failure: I don't see `I luv puppies` anywhere - did you print this?
look_for_rand:
type: php
assertTrue: inputContains('index.php', 'rand(')
failure: I don't see `rand()` function in your file
check_number:
type: http
assertTrue: css("h3 > span").text() matches '/[0-9]+/'
failure: Are you printing the random number inside the `span` tag?
check_range:
type: http
assertTrue: 100 <= css("h3 span").text() and 200 >= css("h3 span").text()
failure: Make sure the `rand()` function only returns numbers between 100 and 200
functions_reverseandlowercasecoding:
question: |
Someone was working at midnight, and started printing out
things in reverse! Don't worry! We've sent them on holiday and
now it's your job to fix things.
question_steps:
- "As a challenge, see if you can reverse this string and set all the characters to lowercase in one line using functions"
step: functions-reverse-and-lowercase
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_strrev:
type: php
assertTrue: inputContains('index.php', 'strrev(')
failure: I don't see `strrev()` function in your file
look_for_strtolower:
type: php
assertTrue: inputContains('index.php', 'strtolower(')
failure: I don't see `strtolower()` function in your file
check_output:
type: http
assertTrue: body() matches "/our pets are purrfectly pawesome!/"
failure: I don't see `our pets are purrfectly pawesome!` anywhere - did you print this?
functions_nestedfunctionsmc:
question: |
Clearly the dev team has been drinking too much Red Bull.
Things have gotten out of hand with all these reversed words.
See if you can figure out what this prints:
```php
<?php echo str_replace('ri', 'aa', strrev(strtolower('SQUIRREL!'))); ?>
```
explanation: |
Think about the order that things happen:
1. `SQUIRREL!` is passed to `strtolower` and becomes `squirrel!`.
2. `squirrel!` is passed to `strrev` and becomes `!lerriuqs`.
3. Finally, the `ri` in `!lerriuqs` is replaced with `aa`, giving us `!leraauqs`.
features:
multiple_choice:
choices:
a: '!lerriuqs'
b: '!lerraaiuqs'
c: '!leraauqs'
d: 'lerriauqs'
correct: c
# ArraysLoops
arraysloops_createarraycoding:
question: |
We're testing out a new feature: find someone to walk your
dog! At first, our boss's kids will be the three dog walkers:
`Kitty`, `Tiger`, and `Jay`.
question_steps:
- Assign each dog walker (`Kitty`, `Tiger`, and `Jay`) to three new variables - `$walker1`, `$walker2` and `$walker3`.
- Put each variable into a new `$dogWalkers` array.
step: arrays-loops-create-array
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
assert_variable_1:
type: php
assertTrue: variable('walker1') == 'Kitty'
failure: Make sure the `$walker1` variable is equal to `Kitty`.
assert_variable_2:
type: php
assertTrue: variable('walker2') == 'Tiger'
failure: Make sure the `$walker2` variable is equal to `Tiger`.
assert_variable_3:
type: php
assertTrue: variable('walker3') == 'Jay'
failure: Make sure the `$walker3` variable is equal `Jay`.
assert_array:
type: php
assertTrue: variable('dogWalkers') == ['Kitty', 'Tiger', 'Jay']
failure: Can't assert that `$dogWalkers` variable equal to the ['Kitty', 'Tiger', 'Jay'] array
arraysloops_foreachcoding:
question: |
The kids are ready to work! Let's advertise the new service.
question_steps:
- Use a `foreach` loop to print each kid's name in an `h3` tag.
- Include a `<button>Schedule me</button>` under each kid's name.
step: arrays-loops-foreach
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_foreach:
type: php
assertTrue: inputContains('index.php', 'foreach')
failure: I don't see `foreach` construct in your file
check_tiger:
type: http
assertTrue: css("h3:contains('Tiger')").count() > 0
failure: Make sure the `Tiger` text is being printed *inside* an `h3` tag
check_kitty:
type: http
assertTrue: css("h3:contains('Kitty')").count() > 0
failure: Make sure the `Kitty` text is being printed *inside* an `h3` tag
check_jay:
type: http
assertTrue: css("h3:contains('Jay')").count() > 0
failure: Make sure the `Jay` text is being printed *inside* an `h3` tag
check_zero_buttons:
type: http
assertFalse: css("body button").count() == 0
failure: Don't forget to add a `<button>Schedule me</button>` inside the `foreach` for each walker!
check_single_button:
type: http
assertFalse: css("body button").count() == 1
failure: I only see *1* `<button>` - make sure to include this *inside* the `foreach` loop so that 3 are printed
check_three_buttons:
type: http
assertTrue: css("body button").count() == 3
failure: There should be *3* `<button>` elements exactly
arraysloops_vardumpcoding:
question: |
The boss is worried that one of his kids was left off the
list!
question_steps:
- Use the `var_dump` function to show him that all three kids are in the array.
step: arrays-loops-var-dump
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_var_dump:
type: php
assertTrue: inputContains('index.php', 'var_dump(')
failure: I don't see `var_dump()` function in your file
check_text:
type: http
assertTrue: body() matches "/Kitty/" and body() matches "/Tiger/" and body() matches "/Jay/"
failure: I don't see `Kitty`, `Tiger` and `Jay` text. Did you "var dump" `$dogWalkers` variable?
arraysloops_arraykeysmc:
question: |
```php
$brands = ['SuperDog', 'Doggie-Os', 'Beef Bites', 'Puppy Gruel', 'Fancy Feast'];
```
Puppies love dinner time! Which option below will print "Puppy Gruel"?
explanation: |
The keys are automatically assigned, starting with *zero*. This means that
`SuperDog` is key `0`, `Doggie-Os` is key `1` and eventually `Puppy Gruel` is
key `3`.
features:
multiple_choice:
choices:
a: echo $brands['Puppy Gruel'];
b: echo $brands(4);
c: echo $brands[4];
d: echo $brands[3];
correct: d
# Arrays2
arrays2_converttoassociativecoding:
question: |
Our favorite pet - Waggy Pig - is a cute white silly dog.
question_steps:
- "Update the code below to use an associative array: set they keys of the array to `name`, `weight`, `age` and `bio`."
- "Then, update the code below so Waggy Pig's bio still prints out!"
step: arrays2-convert-to-associative
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
assert_variable:
type: php
assertTrue: "variable('waggyPig') == {'name': 'Waggy Pig', 'weight': 10, 'age': 7, 'bio': 'Sleepy white fluffy dog'}"
failure: Can't assert that `$waggyPig` variable equal to the ['name' => 'Waggy Pig', 'weight' => 10, 'age' => 7, 'bio' => 'Sleepy white fluffy dog'] array
check_h2:
type: http
assertTrue: css("h2:contains('Waggy Pig')").count() == 1
failure: Make sure the `Waggy Pig` text is being printed *inside* an `h2` tag
check_reversed:
type: http
assertFalse: css("div.age").text() == 10
failure: |
Careful! Be sure to print the `age` key in the `.age` element and the
`weight` key in the `.weight` element. You might have them reversed.
check_age:
type: http
assertTrue: css("div.age").text() == 7
failure: Make sure the *age* (`7`) is being printed *inside* a `div` tag with an `.age` class
check_weight:
type: http
assertTrue: css("div.weight").text() == 10
failure: Make sure the *weight* value (`10`) is being printed *inside* a `div` tag with a `.weight` class
check_paragraph:
type: http
assertTrue: css("p:contains('Sleepy white fluffy dog')").count() == 1
failure: Make sure the `Sleepy white fluffy dog` text is being printed *inside* a `p` tag
arrays2_addnewkeycoding:
question: |
We went to ancestry.com and have discovered Waggy Pig's breed:
`bichon`.
question_steps:
- Add a new `breed` key set to `bichon` on a new line after the `$waggyPig` array has already been created.
- Print this new info below!
step: arrays2-add-new-key
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
check_array_key:
type: php
assertTrue: array_key_exists('breed', variable('waggyPig'))
failure: The `$waggyPig` variable doesn't have a `breed` key!
assert_variable:
type: php
assertTrue: variable('waggyPig')['breed'] == 'bichon'
failure: Can't assert that `$waggyPig['breed']` variable equal to the `bichon` value
look_for_variable:
type: php
assertTrue: inputContains('index.php', "$waggyPig['breed']")
failure: Add the `breed` key *after* the `$waggyPig` variable is created using the `['breed']` syntax
check_breed:
type: http
assertTrue: css(".breed:contains('bichon')").count() == 1
failure: Make sure the `bichon` text is being printed *inside* a tag with a `.breed` class
arrays2_guessautokeymc:
question: |
The dog-walking service is a hit! So, we've added `Bear` to the
list:
```php
<?php
$walker1 = 'Kitty';
$walker2 = 'Tiger';
$walker3 = 'Jay';
$dogWalkers = [$walker1, $walker2, $walker3];
$dogWalkers[] = 'Bear';
?>
```
What array key will PHP automatically assign to `Bear`?
explanation: |
The keys are automatically assigned, starting with *zero*, and when you add a new
item to the array later, it uses the first available number. Since `$walker3`
was assigned the `2` index, `Bear` gets the next available number: `3`.
features:
multiple_choice:
choices:
a: "0" # equal to '0' - really strange, but 0 number don't display in front-end.
b: 3
c: 4
d: Bear
correct: b
# Arrays3
arrays3_multiarraykeyscoding:
question: |
Let's get to know one of the friendliest pets in town!
question_steps:
- Access the `bio` on Pancake and print it inside the `<h1>` tag.
step: arrays3-multi-array-keys
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_variable:
type: php
assertTrue: inputContains('index.php', "$pets[2]['bio']")
failure: "I don't see `$pets[2]['bio']` in your code!"
check_heading:
type: http
assertTrue: css("h1:contains('Breakfast is my favorite!')").count() == 1
failure: Make sure the `Breakfast is my favorite!` text is being printed *inside* an `h1` tag
arrays3_foreachassociativecoding:
question: |
The site is *so* popular that we're adding a store that
sells the world's squeekiest dog toys.
question_steps:
- "Using the `$toys` array below, create a `foreach` statement and print each toy's `name` inside an `h3` tag and its `color` inside an `h4` tag."
- Avoid needing to echo the HTML tags by closing PHP at the end of the `foreach` line.
step: arrays3-foreach-associative
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
check_heading_1:
type: http
assertTrue: css("h3:contains('Bacon Bone')").count() == 1
failure: Make sure the `Bacon Bone` text is being printed *inside* an `h3` tag
check_heading_2:
type: http
assertTrue: css("h3:contains('Tennis Ball')").count() == 1
failure: Make sure the `Tennis Ball` text is being printed *inside* an `h3` tag
check_heading_3:
type: http
assertTrue: css("h4:contains('Bacon-colored')").count() == 1
failure: Make sure the `Bacon-colored` text is being printed *inside* an `h4` tag
check_heading_4:
type: http
assertTrue: css("h4:contains('Yellow')").count() == 1
failure: Make sure the `Yellow` text is being printed *inside* an `h4` tag
arrays3_countandvariablereordercoding:
question: |
We want to impress people with our *large* inventory of toys! Ok, it's not
very big yet... but it will be!
question_steps:
- "Using the `count()` function, print the total number of pet toys that we're selling in the `<h4>` tag."
- "Hint: Be sure the `$toys` variable is defined *before* trying to count it!"
step: arrays3-count-and-variable-reorder
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_count:
type: php
assertTrue: inputContains('index.php', 'count(')
failure: I don't see `count()` function in your file
check_heading:
type: http
assertTrue: css('h4').text() matches '/ 3 /'
failure: I don't see the number `3` inside the `h4` tag. Are you printing the `count()` there?
# FilesJsonBooleans
filesjsonbooleans_jsonencodemc:
question: |
Pop Quiz: `json_encode()` does which of the following with an array?
explanation: |
The first argument to `json_encode` is a PHP array. It then encodes this and
returns the equivalent JSON string:
```php
<?php echo json_encode(['lions', 'tigers', 'bears']); ?>
```
This would print:
```json
[
"lions",
"tigers",
"bears"
]
```
features:
multiple_choice:
choices:
a: Encodes JSON into a PHP array
b: Encodes a PHP array into JSON
c: Reads a JSON file and sets it into a PHP array
correct: b
filesjsonbooleans_readfilejsondecodecoding:
question: |
The toy inventory lives in a `toys.json` file. Great!
question_steps:
- Read the `toys.json` file and use `json_decode` to turn it into a big array.
- Make sure the `foreach` is still correctly printing each toy's details.
- Be careful to make sure the keys from `toys.json` match what you're printing out!
step: files-json-booleans-read-file-json-decode
files:
index.php: ~
toys.json:
read_only: true
features:
editor: ~
browser: ~
grading:
look_for_file_get_contents:
type: php
assertTrue: inputContains('index.php', 'file_get_contents(')
failure: I don't see `file_get_contents()` function in your file
look_for_json_decode:
type: php
assertTrue: inputContains('index.php', 'json_decode(')
failure: I don't see `json_decode()` function in your file
check_h3:
type: http
assertTrue: css("h3:contains('Bacon Bone')").count() > 0
failure: I don't see the `Bacon Bone` text inside the `h3` tag
check_h4:
type: http
assertTrue: css("h4:contains('Bacon-colored')").count() > 0
failure: I don't see the `Bacon-colored` text inside the `h4` tag
filesjsonbooleans_fileputcontentscoding:
question: |
We're going to *totally* troll the cats on the site!
question_steps:
- Use `file_put_contents()` to save the text `Dogs rule!` into a new file called `doglife.txt`.
- Then read that file and print the string in the `h2` tag.
step: files-json-booleans-file-put-contents
files:
index.php: ~
features:
editor: ~
browser: ~
grading:
look_for_file_put_contents:
type: php
assertTrue: inputContains('index.php', 'file_put_contents(')
failure: I don't see `file_put_contents()` function in your file
look_for_doglife.txt:
type: php
assertTrue: inputContains('index.php', 'doglife.txt')
failure: I don't see `doglife.txt` text in your file
look_for_text:
type: php
assertTrue: inputContains('index.php', 'Dogs rule!')
failure: I don't see `Dogs rule!` text in your file
look_for_file_get_contents:
type: php
assertTrue: inputContains('index.php', 'file_get_contents(')
failure: I don't see `file_get_contents()` function in your file
check_heading:
type: http
assertTrue: css("h2:contains('Dogs rule!')").count() == 1
failure: I don't see the `Dogs rule!` text inside the `h2` tag
# IfStatement
ifstatement_ifnotoycolorcoding:
question: |
Oh no! Some of the toys are missing a `color` key and now the page
is giving us a huge error!
question_steps:
- Use an `if` statement to avoid the error that's caused by *some* toys that have no `color` key.
- "If we don't know the color, print `no color`"
step: if-statement-if-no-toy-color
files:
index.php: ~
toys.json:
read_only: true
features:
editor: ~
browser: ~
grading:
look_for_if:
type: php
assertTrue: inputContains('index.php', 'if')
failure: I don't see `if` statement in your file
look_for_array_key_exists:
type: php
assertTrue: inputContains('index.php', 'array_key_exists(')
failure: I don't see `array_key_exists()` function in your file
check_output:
type: http
assertTrue: body() matches '/no color/'
failure: The `Bacon Bone` doesn't have a color, so it *should* say "no color" for that toy.
check_heading:
type: http
assertTrue: css("h4:contains('Yellow')").count() > 0
failure: I don't see the `Yellow` text inside the `h4` tag
ifstatement_ifelseifelsecoding:
question: |
The toys are really getting complicated now, so be careful!
question_steps:
- "Use an `if - elseif - else` statement to handle the following 3 possible situations:"
- If the `color` key exists and is set to `multiple`, print "Multiple Colors".
- If the `color` key does not exist, print "no color"
- Otherwise, print the actual color value
step: if-statement-if-elseif-else
files:
index.php: ~
toys.json:
read_only: true
features:
editor: ~
browser: ~
grading:
look_for_if:
type: php
assertTrue: inputContains('index.php', 'if')
failure: I don't see `if` statement in your file
check_heading_1:
type: http
assertTrue: css("h4:contains('Yellow')").count() == 1
failure: I don't see the `Yellow` text inside the `h4` tag
check_heading_2:
type: http
assertTrue: css("h4:contains('Multiple Colors')").count() == 1
failure: I don't see the `Multiple Colors` text inside the `h4` tag
check_heading_3:
type: http
assertTrue: css("h4:contains('no color')").count() == 1
failure: I don't see the `no color` text inside the `h4` tag
ifstatement_fixerrorsifelsecoding:
question: |
Oh no, management tried coding again. Someone put a password
on the github repo!
question_steps:
- Submit the code to see the error.
- Then find the bug and squash it!
explanation: |
Yes, you squashed all the bugs!
One more piece of advice here: check out [Yoda Conditions](https://en.wikipedia.org/wiki/Yoda_conditions).
These are an alternate style that can help avoid forgetting to use the `==` instead of `=` in
the `if` statements.
The main idea is to reverse the order of the items in your `if` statement - i.e. instead of doing:
```php
if ($variable == 'constant') {
// do something
}
```
Move the constant (i.e. the non-variable) in the beginning of conditional clause:
```php
if ('constant' == $variable) {
// do something
}
```
Why might this be better? Well, it's the same really. But if you accidentally use a
single `=`, PHP will throw a syntax error that's easy to debug. That would have made
this challenge much easier to debug!
step: if-statement-fix-errors-if-else
files:
index.php: ~
toys.json:
read_only: true
features:
editor: ~
browser: ~
grading:
look_for_if:
type: php
assertTrue: inputContains('index.php', 'if')
failure: I don't see `if` statement in your file
check_heading_2:
type: http
assertTrue: css("h4:contains('Yellow')").count() == 1
failure: The Tennis ball is printing as `Surprise Color!`, but it *should* be `Yellow`. There's a more subtle mistake in the first part of the `if` statement...
check_heading_3:
type: http
assertTrue: css("h4:contains('no color')").count() == 1
failure: I don't see the `no color` text inside the `h4` tag only once on a page. Double check conditional clauses
# SystemSetup
systemsetup_dnsfairymc:
question: |
What's the job of the DNS (fairy)?
explanation: |
The DNS converts a domain name to an IP address. With the IP address, the request
can find the right server to go to.
features:
multiple_choice:
choices:
a: To guide the request to the correct server
b: To turn a domain name into an IP address
c: To listen on a port for a request
d: To pass the request to the web server
correct: b
systemsetup_localhostportfilenamemc:
question: |
Suppose I put `http://localhost:8000` into my browser. Which of the following is true:
explanation: |
`localhost` is a special string that resolves to the IP address of the computer you're
using right now. This means that the request leaves our computer, then comes right
back. The `:8000` means that it knocks on the door of port `8000`. Finally, in most
setups, since there is no filename in the URL, `index.php` is executed and returned.
features:
multiple_choice:
choices:
a: |
The request cannot be completed because there is no filename
(e.g. `index.php`) in the URL
b: |
The request uses the DNS to find the IP address of a remote
computer, knocks on the door of port `8000`, and then executes
and returns `index.php`
c: |
The request leaves via port `8000` on my computer, but then
routes back to my computer and executes `index.php`
d: |
The request routes back to port `8000` on my local computer and
`index.php` is executed and returned
correct: d
# CreatingFunctions
creatingfunctions_funwithcommentsmc:
question: |
Put on your thinking cap and look carefully: what does this print?
```php
<?php
/*
* A Functions!
*/
function cat()
{
$dog = dog();
// $dog = strtoupper($dog);
return $dog;
}
/* Another function */
function dog()
{
return 'Molly';
}
$var = cat();
echo $var; echo ' the dog'; // echo '!';
```
explanation: |
Phew! First, we call `cat()`, which calls `dog()` and sets `$dog` to `Molly`. But
the next `strtoupper()` line is commented out with `//` so it *never* runs. This
means that `cat()` returns `Molly`, which is set to `$var`. Finally, we print this
with `echo`, and it *is* legal to have multiple PHP statements on a single line,
as long as each ends with `;`. But, the last `echo '!';` is commented out, and never
runs. That gives us `Molly the dog`.
Oh, and `/*` is another way to start a comment. The difference is that - instead of
commenting out the rest of the line, it comments out everything until it sees a
closing `*/`.
features:
multiple_choice:
choices:
a: Trick question! This will have an error!
b: MOLLY the dog!
c: Molly the dog
d: MOLLY the dog
correct: c
creatingfunctions_createfunctioncoding:
question: |
Being able to read all the toys from `toys.json` is pretty useful, and soon, it
might be handy to re-use this in other places too.
question_steps:
- Create a new `get_great_pet_toys()` function that reads `toys.json`, decodes the contents, and returns the toys.
- Call this to get the toys array.
step: creating-functions-create-function
files:
index.php: ~
toys.json:
read_only: true
features:
editor: ~
browser: ~
grading:
check_function:
type: php
assertTrue: function_exists('get_great_pet_toys')
failure: The `get_great_pet_toys()` function doesn't exist
look_for_function:
type: php
assertTrue: inputContains('index.php', 'get_great_pet_toys(')
failure: I don't see the `get_great_pet_toys()` function in your file
check_heading_1:
type: http
assertTrue: css("h4:contains('Yellow')").count() == 1
failure: Make sure the `Yellow` text is being printed *inside* an `h4` tag
check_heading_2:
type: http
assertTrue: css("h4:contains('Multiple Colors')").count() == 1
failure: Make sure the `Multiple Colors` text is being printed *inside* an `h4` tag
check_heading_3:
type: http
assertTrue: css("h3:contains('Bacon Bone')").count() == 1
failure: Make sure the `Bacon Bone` text is being printed *inside* an `h3` tag
# RequireInclude
requireinclude_isolatefunctionuserequirecoding:
question: |
The pet toy business is really taking off! The site is getting
bigger and we need to stay organized!
question_steps:
- Move the `get_great_pet_toys()` function out of `index.php` and into the new `lib/functions.php` file so we can re-use it later.
step: require-include-isolate-function-use-require
files:
index.php: ~
lib/functions.php: ~
toys.json:
read_only: true
features:
editor: ~
browser: ~
grading:
check_function:
type: php
assertTrue: function_exists('get_great_pet_toys')
failure: The `get_great_pet_toys()` function doesn't exist
look_for_function:
type: php
assertTrue: inputContains('lib/functions.php', 'get_great_pet_toys(')
failure: I don't see the `get_great_pet_toys()` function in the `lib/functions.php` file
look_for_require:
type: php
assertTrue: inputContains('index.php', 'require')
failure: I don't see the `require` statement in the `index.php` file
check_heading:
type: http
assertTrue: css("h3:contains('Bacon Bone')").count() > 0
failure: Make sure the `Bacon Bone` text is being printed *inside* an `h3` tag
requireinclude_noclosingphptagmc:
question: |
Look at the following piece of code:
```php
<?php
$site = 'AirPup';
?>
<?php echo $site;
// end of the file
```
Which is true?
explanation: |
Whenever you open PHP with `<?php`, you need to close it later in that file with `?>`...
**unless** the `?>` would be at the end of the file. In other words, if you forget
to write `?>` at the bottom of the file, PHP basically writes this for you - so it's
not needed. In fact, for reasons you'll learn later, it's best *not* to add the `?>`
at the bottom of the file when you don't need to.
features:
multiple_choice:
choices:
a: This code has no issues. Well done!
b: "This code will have an error: it's missing the closing `?>` at the bottom"
c: This code has no error, but it *should* have a `?>` at the bottom
d: This code will have an error, because you can't put a comment at the bottom of the file
correct: a
# SimpleLayout
simplelayout_layoutwithrequirecoding:
question: |
The site has just one page - `index.php` - and people *love* it (trust us)!
Now we've created a second page - `aboutUs.php`, but boy is it ugly! Woof.
We need to re-use the layout that's in `index.php` to make it prettier.
question_steps:
- Move all the header HTML into `header.php` and move all the footer HTML into `footer.php`.
- Then, `require` each of these files in `index.php` and `aboutUs.php` to get the same, "nice" layout in both of our pages.
step: simple-layout-layout-with-require
files:
index.php: ~
aboutUs.php: ~
layout/header.php: ~
layout/footer.php: ~
features:
editor: ~
browser:
default_uri: /aboutUs.php
grading:
look_for_html_opening_tag:
type: php
assertTrue: inputContains('layout/header.php', '<html>')
failure: I don't see `html` opening tag in `layout/header.php` file
look_for_html_closing_tag:
type: php
assertTrue: inputContains('layout/footer.php', '<\/html>')
failure: I don't see `html` closing tag in `layout/footer.php` file
look_for_variable:
type: php
assertTrue: inputContains('index.php', '$airpupTagLine =')
failure: "Keep the `$airpupTagLine = ...` code inside `index.php`: that's code that is used for just *this* page, and shouldn't be in the header"
look_for_require:
type: php
assertTrue: inputContains('index.php', 'require')
failure: I don't see `require` statement in `index.php` file
# @TODO Dont know how to converts next grading:
# $htmlGrader->assertOutputContains('<html>', 'Don\'t forget to require the `header.php` file in `aboutUs.php` so that it has the nice layout');
# $htmlGrader->assertOutputContains('</html>', 'Don\'t forget to require the `footer.php` file in `aboutUs.php` so that it has the nice layout');
simplelayout_requireandvariablesmc:
question: |
Suppose your `index.php` file creates a variable and includes a `header.php` file:
```php
<!-- index.php -->
<?php
$secretWord = 'engage';
require 'header.php';
?>
```
Inside of `header.php`, what would happen if you tried to access this `$secretWord` variable?
```php
<!-- header.php -->
<header>
<?php echo $secretWord; ?>
</header>
```
explanation: |
When you `require` or `include` a file, all variables that you currently have access
to - like `$secretWord` - are still accessible in the new file. When you use `require`,
it's almost like someone copies the contents of `header.php` and pastes it into
`index.php`.
features:
multiple_choice:
choices:
a: This would print `engage` in the `<header>` tag
# String with colon inside should be quoted:
b: "This would cause an error: the `$secretWord` variable is defined in `index.php`, not in `header.php`"
c: There would be no error, but nothing would print inside the `<header>` tag
correct: a
# Episode 2
# RequestNewPage
requestnewpage_requestpostdatamc:
question: |
When you go to a site, your browser makes a request for that page, and even
sends extra information, like what language you prefer. A request to
`pets_new.php` might look like this:
```
POST /pets_new.php
Host: localhost:8000
Accept-Language: en-US,en;q=0.8
pet_name=Waggy%20Pig&breed=bichon
```
Which of the following most accurately describes this request?
explanation: |