loading...

re: Daily Challenge #1 - String Peeler VIEW POST

FULL DISCUSSION
 

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]
}
 

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.

code of conduct - report abuse