TheOdinProject / ruby-exercises

MIT License
227 stars 1.09k forks source link

Fix test case for #find_favorite in nested array exercises #64

Closed JoshDevHub closed 2 years ago

JoshDevHub commented 2 years ago

In the Odin discord, someone posted the following "solution" to the #find_favorite exercise in nested_array_exercises.rb where the goal is to write a method that takes an array of hash objects and returns the hash object with the key: value pair of is_my_favorite?: true -- or nil if no such hash object exists.

def find_favorite(array_of_hash_objects)
  array_of_hash_objects.each do |data|
    if data[:is_my_favorite?] == true
      return data
    else
      return nil
    end
  end
end

The case that's true for the first element in array_of_hash_objects is what will get returned from the method, which doesn't solve the problem of returning the hash with the key: value pair of is_my_favorite?: true. However since the tests for this method (which are here) only ever provide an array with a hash holding :is_my_favorite?: true at the 0th index, the tests pass for this solution just fine. I'd like to submit a PR that properly fails the above code by either:

  1. Adding a test case that has the { name: 'Ruby', is_my_favorite?: true } hash at a non-zero index of the let array.
  2. Just changing the existing test case to place the { name: 'Ruby', is_my_favorite?: true} at a non-zero index of the let array. I think it could be fine to just stick with the one case and place it in the middle of the array at index 1, as it seems unlikely someone would stumble into writing something that passes with it in the middle of the array while also not being a true solution to the problem.

Let me know what you think πŸ‘

rlmoser99 commented 2 years ago

@JoshDevHub Thanks for making this issue. I think changing the existing test case is the best option, as you explained. When you make a PR, please reference this issue. πŸ‘