coding for food
-
[Ruby] Reversecoding for food 2022. 7. 23. 13:00
Write a method reverse(word) that takes in a string word and returns the word with its letters in reverse order. my code def reverse(word) rev = "" i = word.length while i > 0 i -= 1 rev += word[i] end return rev end puts reverse("cat") # => "tac" solution def reverse(word) reversed = "" i = 0 while i < word.length char = word[i] reversed = char + reversed i += 1 end return reversed end puts rev..
-
[Ruby] Sum Numscoding for food 2022. 7. 23. 03:32
my code def sum_nums(max) i = 0 sum = 0 while i 10, because 1 + 2 + 3 + 4 = 10 puts sum_nums(5) # => 15 solution def sum_nums(max) sum = 0 i = 1 while i 10 puts sum_nums(5) # => 15 1. sum_nums(4), 구해야 하는 답이 1+2+3+4 = 10 인데 처음 시작이 1부터 max 4 이기 때문에 i = 1 로 시작하고 while에서의 조건 i 는 max 보다 작거나 같아야 한다. I wanna take i and add it to my sum, but..
-
[Ruby] Count Ecoding for food 2022. 7. 22. 11:49
Count E Write a method count_e(word) that takes in a string word and returns the number of e's in the word def count_e(word) count = 0 # use count to track the number of e's i = 0 # use i to iterate thru the word while i 1 puts count_e("excellent") # => 3
-
-
[Ruby] Conditionals exercisescoding for food 2022. 7. 21. 08:21
my code 1 def either_only(number) if number % 15 == 0 return false elsif number % 3 == 0 return true elsif number % 5 == 0 return true else return false end end puts either_only(9) # => true puts either_only(20) # => true puts either_only(7) # => false puts either_only(15) # => false puts either_only(30) # => false solution 2 def either_only(number) if (number % 3 == 0 || number % 5 == 0) && !(n..