Skip to content

Instantly share code, notes, and snippets.

View itzsaga's full-sized avatar
🍕

Seth itzsaga

🍕
View GitHub Profile
def oxford_comma(array)
if array.length == 1
return "#{array[0]}"
elsif array.length == 2
return array.join(" and ")
elsif array.length >= 3
new_last_array_item = "and #{array[-1]}"
array.pop
array.push(new_last_array_item)
return array.join(", ")
def oxford_comma(array)
if array.length == 1
return "#{array[0]}"
elsif array.length == 2
return array.join(" and ")
elsif array.length >= 3
array[-1] = "and #{array[-1]}"
return array.join(", ")
end
end
# FIRST METHOD THAT PASSED
def reverse_each_word(array)
new_array = array.split(" ")
reversed_array = new_array.each {|x| x.reverse!}
return reversed_array.join(" ")
end
# FIRST REFACTORED CODE
def reverse_each_word(array)
new_array = array.split(" ")
def prime?(n)
if n <= 1
return false
elsif n <= 3
return true
else (2..n/2).none? do |x|
n % x == 0
end
end
end
def swap_elements_from_to(array, index, new_index)
array[index], array[new_index] = array[new_index], array[index]
return array
end
# Question 4 Bonus
describe 'swap_elements_from_to' do
it 'swaps elements and allows you to specify the index of the element you would like to move to a new index' do
expect(swap_elements_from_to(["one", "two", "three"], 2, 1)).to eq(["one", "three", "two"])
end
end
NoMethodError:
undefined method `starts_with?' for "apple":String
Did you mean? start_with?
# given that holiday_hash looks like this:
# {
# :winter => {
# :christmas => ["Lights", "Wreath"],
# :new_years => ["Party Hats"]
# },
# :summer => {
# :fourth_of_july => ["Fireworks", "BBQ"]
# },
# :fall => {
def all_holidays_with_bbq(holiday_hash)
holiday_hash.map do |season, holidays|
holidays.map do |holiday_name, supplies
supplies.include?("BBQ") ? holiday_name : nil
end
end.flatten.compact
end
module Players
class Computer < Player
def move(board)
possible_moves = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
valid_moves = []
possible_moves.each do |move|
valid_moves << move if board.cells[move.to_i-1] = " "
end
valid_moves.sample
end