coding for food

[Ruby] exercise _ Is Valid Email

silveryen 2022. 8. 2. 07:07
# For simplicity, we'll consider an email valid when it satisfies all of the following:
# - contains exactly one @ symbol
# - contains only lowercase alphabetic letters before the @
# - contains exactly one . after the @

def is_valid_email(str)

end

puts is_valid_email("abc@xy.z")         # => true
puts is_valid_email("jdoe@gmail.com")   # => true
puts is_valid_email("jdoe@g@mail.com")  # => false
puts is_valid_email("jdoe42@gmail.com") # => false
puts is_valid_email("jdoegmail.com")    # => false
puts is_valid_email("az@email")         # => false

 

my code = solution

def is_valid_email(str)
  
  email = str.split("@")
  
  if email.length !=2
    return false
  end
  
  first = email[0]
  second = email[1]
  lowercase = "abcdefghijklmnopqrstuvwxyz"
  
  first.each_char do |char|
  	if !lowercase.include?(char)
      return false
    end
  end
  
  if second.split(".").length == 2
    return true
  else
    return false
  end

end