Array that alphabetizes and alternates between all caps

后端 未结 5 992
面向向阳花
面向向阳花 2020-12-22 10:44

I need to get the user to input five words, which I believe I have. Then the program needs to spit the words in alphabetical order with every other word being in all-caps, s

5条回答
  •  失恋的感觉
    2020-12-22 11:15

    If I understand well (but the question is not so clear) you want to sort and then put one word out of two in uppercase and the other in lowercase:

    words.sort.each_with_index.map{|w,i| i.odd? ? w.upcase : w.downcase }
    

    Test:

    words=%w( orange banana apple melon)
    

    Result:

    ["APPLE", "banana", "MELON", "orange"]
    

提交回复
热议问题