-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdogs.rb
69 lines (58 loc) · 1.73 KB
/
dogs.rb
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
require_relative "testing_library"
dogs = ["Fido", "Harleigh", "Mali", "Trixie", "Snow", "Victory"]
def how_many_dogs(dogs)
dogs.length
end
def name_lengths(dogs)
new_arr = []
dogs.each do |dog|
new_arr.push(dog.length)
end
new_arr
end
def reverse_dog_names(dogs)
new_arr = []
dogs.each do |dog|
new_arr.push(dog.reverse)
end
new_arr
end
def first_three_dogs_with_each(dogs)
new_arr = []
iter = 0
dogs.each do |dog|
if iter >=3
return new_arr
end
new_arr.push(dog)
iter += 1
end
end
def first_three_dogs_without_each(dogs)
dogs[0..2]
end
def reverse_case_dog_names(dogs)
new_arr = []
dogs.each {|dog| new_arr.push(dog.swapcase) }
new_arr
end
# single line of code no each
def sum_of_all_dog_name_lengths(dogs)
dogs.join.length
end
# no each
def dogs_with_long_names(dogs)
dogs.collect {|dog| dog.length > 4}
end
puts "*"*80
puts "Make each method return the correct value"
puts "The check method will run and tell you if the answer is correct"
puts "*"*80
check("how_many_dogs", how_many_dogs(dogs) == 6)
check("name_lengths", name_lengths(dogs) == [4, 8, 4, 6, 4, 7])
check("reverse_dog_names", reverse_dog_names(dogs) == ["odiF", "hgielraH", "ilaM", "eixirT", "wonS", "yrotciV"])
check("first_three_dogs_with_each", first_three_dogs_with_each(dogs) == ["Fido", "Harleigh", "Mali"])
check("first_three_dogs_without_each", first_three_dogs_without_each(dogs) == ["Fido", "Harleigh", "Mali"])
check("reverse_case_dog_names", reverse_case_dog_names(dogs) == ["fIDO", "hARLEIGH", "mALI", "tRIXIE", "sNOW", "vICTORY"])
check("sum_of_all_dog_name_lengths", sum_of_all_dog_name_lengths(dogs) == 33)
check("dogs_with_long_names", dogs_with_long_names(dogs) == [false, true, false, true, false, true])