DEV Community

Discussion on: Daily Challenge #1 - String Peeler

Collapse
 
rvictorino profile image
Robin Victorino • Edited

Hi!

A bit late to the party, but here's a Groovy one:

String peel(String toPeel) {
    if(!(toPeel?.length() > 2)) {
        throw new Exception('Can\'t peel a string shorter than 3 characters')
    }
    return toPeel[1..-2]
}
Collapse
 
flamangoes profile image
flamangoes

My approach in groovy...

Not sure if "ignore <2 chars" means "don't consider" or "ignore the trimming"

Don't consider =>

String peel(String toPeel) {
    toPeel[1..-2]
}

Ignore =>

String peel(String toPeel) {
    toPeel?.length() >2 ? toPeel[1..-2] : toPeel
}

I don't like returning null.