Loops Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Count E

Write a method count_e(word) that takes in a string word and returns the number of e’s in the word

A
def count_e(word)
  count = 0

i = 0
while i < word.length
char = word[i]

if char == "e"
  count += 1
end

i += 1   end

return count
end

puts count_e("movie") # => 1
puts count_e("excellent") # => 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Count A
Write a method count_a(word) that takes in a string word and returns the number of a’s in the word. The method should count both lowercase (a) and uppercase (A)

A
def count_a(word)
  count = 0

i = 0
while i < word.length
char = word[i]

if char == "a" || char == "A"
  count += 1
end

i += 1   end

return count
end

puts count_a("application")  # => 2
puts count_a("bike")         # => 0
puts count_a("Arthur")       # => 1
puts count_a("Aardvark")     # => 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Count Vowels
Write a method, count_vowels(word), that takes in a string word and returns the number of vowels in the word. Vowels are the letters a, e, i, o, u.

A
def count_vowels(word)
  num_vowels = 0

i = 0
while i < word.length
char = word[i]

if char == "a" || char == "e" || char == "i" || char == "o" || char == "u"
  num_vowels += 1
end

i += 1   end

return num_vowels
end

puts count_vowels("bootcamp")  # => 3
puts count_vowels("apple")     # => 2
puts count_vowels("pizza")     # => 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sum Nums
Write a method sum_nums(max) that takes in a number max and returns the sum of all numbers from 1 up to and including max.

A
def sum_nums(max)
  sum = 0

i = 1
while i <= max
sum += i

i += 1   end

return sum
end

puts sum_nums(4) # => 10
puts sum_nums(5) # => 15
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Factorial
Write a method factorial(num) that takes in a number num and returns the product of all numbers from 1 up to and including num.

A
def factorial(num)
  product = 1

i = 1
while i <= num
product *= i

i += 1   end

return product
end

puts factorial(3) # => 6, because 1 * 2 * 3 = 6
puts factorial(5) # => 120, because 1 * 2 * 3 * 4 * 5 = 120
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Reverse

Write a method reverse(word) that takes in a string word and returns the word with its letters in reverse order.

A
def reverse(word)
  reversed = ""

i = 0
while i < word.length
char = word[i]
reversed = char + reversed

i += 1   end

return reversed
end

puts reverse("cat")          # => "tac"
puts reverse("programming")  # => "gnimmargorp"
puts reverse("bootcamp")     # => "pmactoob"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is Palindrome
Write a method is_palindrome(word) that takes in a string word and returns the true if the word is a palindrome, false otherwise. A palindrome is a word that is spelled the same forwards and backwards.

A
def is_palindrome(word)
  reversed = ""

i = 0
while i < word.length
char = word[i]
reversed = char + reversed

i += 1   end

return reversed == word
end

puts is_palindrome("racecar")  # => true
puts is_palindrome("kayak")    # => true
puts is_palindrome("bootcamp") # => false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly