Password validator

I chose the password validator in the code coach and even I tried so hard to write the code,I couldn’t satisfied case 10 and case 11,then I asked for help on stackoverflow then 1 man helped me with his code but still,it didn’t satisfy the case 10 and 11.Can anybody helps me with this? This is the code: password=gets.chomp symbols = %w(! @ # $ % & *) counter = 0 symbols.each do |element| if password.include?(element) counter += 1 end end if password.length >= 7 && password.tr("^0-9 ", "").length >= 2 && counter >= 2 puts "Strong" else puts "Weak" end

5th Mar 2020, 9:00 AM
Minh Tùng
Minh Tùng - avatar
2 Answers
+ 1
You shouldn't check whether each special character exists in the string. Instead you should count its number, and check if number of special characters exceeds 2, just like how you handle checking for number of numbers. Test your program with "abcd11!!"
5th Mar 2020, 9:17 AM
Gordon - avatar
+ 1
You can try changing your approach to solve the challenge. Iterate through the string and count the numbers of symbols and numbers. Then only output "Strong" if the amount of symbols and numbers is 2 or more, and the string length is 7 or more.
6th Mar 2020, 1:41 PM
かんでん - avatar