0

[SOLVED] I don't know what is wrong with it. What is the problem?

It works when all letters in the inputted string are lowercase but when I input a string with an uppercase letter it does not work. I even tried to use the downcase method but it still causes an error. Input - abc Input - Abc Output - [1, 2, 3] Output - Error https://code.sololearn.com/cb71hiiODPta/?ref=app

16th Feb 2021, 3:14 PM
aka :)
aka :) - avatar
2 Antworten
+ 4
This is because you are calling .index() on an array which only contains letters from lowercase 'a' till lowercase 'z', that is, the array does not have uppercase letters. Passing a letter to arrayofnums.index() that is not present in arrayofnums (like an uppercase letter) will return `nil`. When you use the + operator on nil, it gives error. `word.downcase` does not work for you because the `String.downcase` method does NOT make changes to the original string, it instead *returns the downcased string*. Change the line 4 to `word = word.downcase` OR `word.downcase!`
16th Feb 2021, 3:50 PM
XXX
XXX - avatar
+ 1
XXX Thank you mate. It has been fixed. 😊👍 I also understand what the problem was as a bonus. Thanks!
16th Feb 2021, 3:54 PM
aka :)
aka :) - avatar