Skip to content

Commit

Permalink
Complete TheOdinProject#6, arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
JCFarrow committed Jun 7, 2022
1 parent b0ccd9d commit a5d223a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
14 changes: 14 additions & 0 deletions ruby_basics/6_arrays/exercises/array_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
def nil_array(number)
# return an array containing `nil` the given number of times
Array.new(number)
end

def first_element(array)
# return the first element of the array
array[0]
end

def third_element(array)
# return the third element of the array
array[2]
end

def last_three_elements(array)
# return the last 3 elements of the array
array.last(3)
end

def add_element(array)
# add an element (of any value) to the array
array.push(1)
end

def remove_last_element(array)
# Step 1: remove the last element from the array
array.pop

# Step 2: return the array (because Step 1 returns the value of the element removed)
array
end

def remove_first_three_elements(array)
# Step 1: remove the first three elements
array.shift(3)

# Step 2: return the array (because Step 1 returns the values of the elements removed)
array
end

def array_concatenation(original, additional)
# return an array adding the original and additional array together
original + additional
end

def array_difference(original, comparison)
# return an array of elements from the original array that are not in the comparison array
original - comparison
end

def empty_array?(array)
# return true if the array is empty
array.empty?
end

def reverse(array)
# return the reverse of the array
array.reverse
end

def array_length(array)
# return the length of the array
array.size
end

def include?(array, value)
# return true if the array includes the value
array.include?(value)
end

def join(array, separator)
# return the result of joining the array with the separator
array.join(separator)
end
54 changes: 27 additions & 27 deletions ruby_basics/6_arrays/spec/array_exercises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,152 +9,152 @@
end

# remove the 'x' from the line below to unskip the test
xit 'returns an array containing 2 nil values' do
it 'returns an array containing 2 nil values' do
expect(nil_array(2)).to eq([nil, nil])
end
end

describe 'first element exercise' do

xit 'returns the first element of an array of numbers' do
it 'returns the first element of an array of numbers' do
expect(first_element([2, 4, 6, 8, 10])).to eq(2)
end

xit 'returns the first element of an array of strings' do
it 'returns the first element of an array of strings' do
expect(first_element(['foo', 'bar'])).to eq('foo')
end
end

describe 'third element exercise' do

xit 'returns the third element of an array of numbers' do
it 'returns the third element of an array of numbers' do
expect(third_element([2, 4, 6, 8, 10])).to eq(6)
end

xit 'returns nil if the array does not have a third element' do
it 'returns nil if the array does not have a third element' do
expect(third_element(['foo', 'bar'])).to eq(nil)
end
end

describe 'last three elements exercise' do

xit 'returns an array of the last three elements' do
it 'returns an array of the last three elements' do
expect(last_three_elements([2, 4, 6, 8, 10])).to eq([6, 8, 10])
end

xit 'returns all of the elements when there are less than 3 elements' do
it 'returns all of the elements when there are less than 3 elements' do
expect(last_three_elements(['foo', 'bar'])).to eq(['foo', 'bar'])
end
end

describe 'add element exercise' do

xit 'increases the length of an array by 1' do
it 'increases the length of an array by 1' do
numbers = [1, 2, 3, 4]
expect { add_element(numbers) }.to change { numbers.length }.by(1)
end

xit 'increases the length of an empty array by 1' do
it 'increases the length of an empty array by 1' do
data = []
expect { add_element(data) }.to change { data.length }.by(1)
end
end

describe 'remove last element exercise' do

xit 'returns the array without the last element' do
it 'returns the array without the last element' do
expect(remove_last_element([1, 3, 5])).to eq([1, 3])
end

xit 'returns an empty array when the array only has one element' do
it 'returns an empty array when the array only has one element' do
expect(remove_last_element(['foo'])).to eq([])
end
end

describe 'remove first three elements exercise' do

xit 'returns the array without the first three elements' do
it 'returns the array without the first three elements' do
expect(remove_first_three_elements([1, 3, 5, 7, 9])).to eq([7, 9])
end

xit 'returns an empty array when the array has less than 3 elements' do
it 'returns an empty array when the array has less than 3 elements' do
expect(remove_first_three_elements(['foo', 'bar'])).to eq([])
end
end

describe 'array concatenation exercise' do

xit 'returns an array adding two arrays of numbers together' do
it 'returns an array adding two arrays of numbers together' do
expect(array_concatenation([1, 3, 5], [2, 4, 6])).to eq([1, 3, 5, 2, 4, 6])
end

xit 'returns an array adding arrays of strings and numbers together' do
it 'returns an array adding arrays of strings and numbers together' do
expect(array_concatenation(['a', 'b', 'c'], [1, 2, 3])).to eq(['a', 'b', 'c', 1, 2, 3])
end
end

describe 'array difference exercise' do

xit 'returns an array subtracting two arrays of numbers' do
it 'returns an array subtracting two arrays of numbers' do
expect(array_difference([0, 1, 1, 2, 3, 5], [0, 1, 2])).to eq([3, 5])
end

xit 'returns an array subtracting two arrays of strings' do
it 'returns an array subtracting two arrays of strings' do
expect(array_difference(['foo', 'bar', 'baz'], ['bar','hello'])).to eq(['foo', 'baz'])
end
end

describe 'empty array exercise' do

xit 'returns true when the array is empty' do
it 'returns true when the array is empty' do
expect(empty_array?([])).to be true
end

xit 'returns false when the array is not empty' do
it 'returns false when the array is not empty' do
expect(empty_array?([1, 2, 3])).to be false
end
end

describe 'reverse exercise' do

xit 'returns an array containing the elements in reverse order' do
it 'returns an array containing the elements in reverse order' do
expect(reverse([0, 1, 1, 2, 3, 5])).to eq([5, 3, 2, 1, 1, 0])
end

xit 'returns an array containing the element when there is only one' do
it 'returns an array containing the element when there is only one' do
expect(reverse(['foo'])).to eq(['foo'])
end
end

describe 'array length exercise' do

xit 'returns the length of the array' do
it 'returns the length of the array' do
expect(array_length([0, 1, 1, 2, 3, 5])).to eq(6)
end

xit 'returns zero when the array is empty' do
it 'returns zero when the array is empty' do
expect(array_length([])).to eq(0)
end
end

describe 'include exercise' do

xit 'returns true when the array contains the specified value' do
it 'returns true when the array contains the specified value' do
expect(include?([0, 1, 1, 2, 3, 5], 3)).to be true
end

xit 'returns false when the array does not contain the specified value' do
it 'returns false when the array does not contain the specified value' do
expect(include?([0, 1, 1, 2, 3, 5], 8)).to be false
end
end

describe 'join exercise' do

xit 'returns a string joining an array of numbers with " + "' do
it 'returns a string joining an array of numbers with " + "' do
expect(join([0, 1, 1, 2, 3, 5], ' + ')).to eq('0 + 1 + 1 + 2 + 3 + 5')
end

xit 'returns a string joining an array of strings with " "' do
it 'returns a string joining an array of strings with " "' do
expect(join(['foo', 'bar', 'baz'], ' ')).to eq('foo bar baz')
end
end
Expand Down

0 comments on commit a5d223a

Please sign in to comment.