Skip to content

Answers questions 1-36#207

Open
tobywinter wants to merge 1 commit intomakersacademy:masterfrom
tobywinter:master
Open

Answers questions 1-36#207
tobywinter wants to merge 1 commit intomakersacademy:masterfrom
tobywinter:master

Conversation

@tobywinter
Copy link

Corrects a couple of errors/typos in the questions.

# where 'special character' means anything apart from the letters
# a-z (uppercase and lower) or numbers
def check_a_string_for_special_characters(string)
/\W/ === string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverse the order of the operands /\W/ === string.

# [1, 3, 5, 4, 1, 2, 6, 2, 1, 3, 7]
# becomes [1, 3, 5, 4, 1, 2]
def get_elements_until_greater_than_five(array)
array.take_while { |element| element <= 5}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing inside }.

# return the shortest word in an array
# return the longest word in an array
def longest_word_in_array(array)
array.max { |a,b| a.length <=> b.length }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use max_by(&:length) instead of max { |a, b| a.length <=> b.length }.
Space missing after comma.

# even numbers come first
# so [1, 2, 3, 4, 5, 6] becomes [[2, 4, 6], [1, 3, 5]]
def separate_array_into_even_and_odd_numbers(array)
[array.select { |el| el.even? }, array.select { |el| el.odd? } ]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside square brackets detected.

# turn a positive integer into a negative integer. A negative integer
# stays negative
def make_numbers_negative(number)
-(number.abs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use parentheses around a method call.

# round up - so 'apple' becomes 'app'
def get_first_half_of_string(string)
midpoint = (string.length.to_f / 2).ceil
string[0...(midpoint)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use parentheses around a variable.

# discard the first 3 elements of an array,
# e.g. [1, 2, 3, 4, 5, 6] becomes [4, 5, 6]
def all_elements_except_first_3(array)
array.select {|el| array.index(el) > 2 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between { and | missing.

# don't reverse the array, but reverse every word inside it. e.g.
# ['dog', 'monkey'] becomes ['god', 'yeknom']
def reverse_every_element_in_array(array)
array.map { |value| value.reverse }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

@@ -1,152 +1,182 @@
# keep only the elements that start with an a
def select_elements_starting_with_a(array)
array.select { |word| word[0].downcase == 'a' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use casecmp instead of downcase ==.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants