forked from pgrunde/array-warmup-easy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogs.rb
83 lines (58 loc) · 1.74 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require_relative "testing_library"
dogs = ["Fido", "Harleigh", "Mali", "Trixie", "Snow", "Victory"]
def how_many_dogs(dogs)
dogs.length
end
def name_lengths(dogs)
dogs.collect do |dog|
dog.length
end
end
def reverse_dog_names(dogs)
dogs.map { |dog| dog.reverse }
end
def first_three_dogs_with_each(dogs)
first_three = []
dogs.each_with_index do |dog, index|
if index == 3
return first_three
end
first_three.push(dog)
end
end
def first_three_dogs_without_each(dogs)
dogs[0..2]
end
def reverse_case_dog_names(dogs)
dogs.map { |dog| dog.swapcase }
end
def sum_of_all_dog_name_lengths(dogs)
dog_sum = 0
dogs.each do |dog|
dog_sum = dog_sum + dog.length
end
dog_sum
end
def dogs_with_long_names(dogs)
long = []
dogs.each do |dog|
if dog.length > 4
long << true
else
long << false
end
end
long
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])