0

When str[0] is "a" and str[4] is "b" I get true. However, when "a" is in another position and is separated by three spaces from "b" I get false.

Any help would be great!

def ABCheck(str)

  str.downcase.split()
  x = 0
  while x < str.length - 4
    return true if ((str[x] == "a") && (str[x + 4] =="b"))
      x += 1
    return false

  end     
end

puts ABCheck("azzzb")
#Currently puts "true"
puts ABCheck("bzzabzcb")
#Currently puts "false" - even though it should print true

2 Answers 2

4

That's because return false is called before you expected. It should be put outside the loop:

def ABCheck(str)
  str.downcase.split()
  x = 0
  while x < str.length - 4
    return true if ((str[x] == "a") && (str[x + 4] =="b"))
    x += 1
  end  
  return false   
end
Sign up to request clarification or add additional context in comments.

Comments

0

You're calling false before your while loops is complete. You need to call it after the while loop.

Comments

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.