+ 1

Military Time in Ruby

My code: a=gets b=a[0..4] ans="" if a[-2]=="P" if b[1]==":" ans=(b[0])+12+b[1...] else if b[0...2]== "12" ans="12"+b[2...] else ans=((b[0..2]).to_i+12).to_s+b[2...] end end else if b[1]==":" ans="0"+b[0...] else if b[0..2]=="12" ans="00"+b[2...] else ans=b end end end print(ans) Keep getting the #4 case wrong. Does anybody have any ideas?

29th Apr 2021, 9:59 PM
Rômulo Ramos Pereira
Rômulo Ramos Pereira - avatar
4 Respostas
+ 1
Hi! your program does not work with the value 1: 15 PM
30th Apr 2021, 6:26 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Military Time in Ruby My code: a=gets b=a[0..4] ans="" if a[-2]=="P" if b[1]==":" ans=((b[0]).to_i+12).to_s+b[1...] else if b[0...2]== "12" ans="12"+b[2...] else ans=((b[0..2]).to_i+12).to_s+b[2...] end end else if b[1]==":" ans="0"+b[0...] else if b[0..2]=="12" ans="00"+b[2...] else ans=b end end end print(ans)
5th May 2021, 10:49 PM
Rômulo Ramos Pereira
Rômulo Ramos Pereira - avatar
0
Good! This code pass all tests!
6th May 2021, 6:10 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
input = gets.chomp if input.include?("AM") if input[1] == ":" time = "0" + input[0] + ":" + input[2..3] else time = input[0..1] + ":"+ input[3..4] end else if input[1] == ":" time = (input[0].to_i + 12).to_s + ":" + input[2..3] else time = (input[0..1].to_i + 12).to_s + ":"+ input[3..4] end end puts time
14th May 2021, 4:40 PM
Mohamed Habib
Mohamed Habib - avatar