I got a lot done this morning. Especially after the coffee kicked in. When I started I was definitely not 100% awake. I even did some extra credit in a lab which I typically do not do. I’ve never been the type to want to score 105 on a test because I aced it AND the extra credit. I did this one specifically because I knew it wouldn’t take much time as I was writing a generic method for a specific method I had just completed. Basically inserting placeholders where the original problem had specific items.
So first I solved this:
Advanced: Try building a method swap_elements_from_to
that takes three arguments, array, index, destination_index
, that will allow you to specify the index of the element you would like to move to a new index.
Like this:
def swap_elements_from_to(array, index, new_index)
array[index], array[new_index] = array[new_index], array[index]
return array
end
Then I did this:
Advanced #2: Try writing test coverage for it!
Like this:
# 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
And it worked!
I successfully wrote my first test! I was stoked! I know that I’ll have to write my own tests in the future so this small win was nice.
As I kept on working, I must say, the fact that I got this as an error is awesome:
NoMethodError:
undefined method `starts_with?' for "apple":String
Did you mean? start_with?
I can definitely see why test-driven development (TDD) makes so much sense. I had been thinking it was almost like doing double the work but I could’ve been stuck there forever because of one letter.
Time spent today: 2:39
Time spent total: 41:07
Lessons completed today: 9
Lessons completed total: 218