0

I am trying to write code that takes in an array of strings and returns the strings that are either less than 6 or end in "y", but not both.

The problem does not want a new array returned. I wrote code that works if I were to return a new array of strings that fit the conditions, but if I try to return just the strings and not an array, it doesn't work.

# frozen_string_literal: true

def select_long_words(words)
  str = []

  i = 0

  while i < words.length

    word = words[i]

    if (word.length < 6 || word[-1] == 'y') && !(word.length < 6 && word[-1] == 'y')

      str << word

    end

    i += 1

  end

  str
end

print select_long_words(%w[whatever are butterfly wit foreward funny])

puts

print select_long_words(%w[keepers cody])

This is the code that returns the new array of strings that fit the conditions.

4
  • 1
    "it doesn't work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Commented Jun 17, 2019 at 16:18
  • You are right. My apologies for not providing more detail- this was my first post. Commented Jun 17, 2019 at 16:32
  • To highlight code as you see in other questions, do the following. If the code is within text, surround it with backticks ("...because the method `zombie` returns...", which appears as "...because the method zombie returns..."). To highlight and indent a block of code, either indent it four spaces or select it and click on the icon {} above the edit box. Try it by editing your question now. To add to Jörg's comment, if an exception is raised tell us the line in which the error occurred, which is part of the error message. Commented Jun 17, 2019 at 20:21
  • This sounds like a homework question. If so, I'd recommend reading "How do I ask and answer homework questions?". Commented Jun 18, 2019 at 1:38

1 Answer 1

2

Your question is a bit unclear but I'll try to anwser.

To select words that are less than 6 caracters or (exclusively) end with "y" you can use this function:

def select_long_words(words)
  words.select do |word|
    is_less_than_6 = word.size < 6
    ends_with_y = word.end_with?('y')
    # Select only if one of the two condition is respected, not both or none
    next is_less_than_6 ^ ends_with_y
  end
end

In this function I used the select function from Array that stands for "select every element that corresponds to the given criteria" and I set the criteria to "less than 6 characters or ends with 'y' but not both" using ^ which mean xor for booleans.

If you want only one word you can call your function like this:

select_long_words(["whatever", "are", "butterfly", "wit", "foreward", "funny"]).first

It'll return nil if there's no correspondancies, or the first word that corresponds. You can replace select with find in the method to get the first result directly.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This was very helpful. I am new to coding, so I was just unaware of some of the syntax
You're welcome :) If you need to do/learn things with arrays take a look to the Ruby documentation : - ruby-doc.org/core-2.6.3/Enumerable.html - ruby-doc.org/core-2.6.3/Array.html It contain a lot of core method that can help you (with a lot of example as well).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.