DEV Community


Discussion on: Daily Challenge #190 - capitalizeFirstLast

tails128 profile image

The most straightforward way to fix this is probably to do something as:

export const capitalizeFirstLast = (inputString: string): string => 
        .split(' ')
        .reduce((accumulator: string, entry: string) =>
                let fixedWord: string = capitalizeFirstLastInWord(entry);

                return `${accumulator} ${fixedWord}`;
            }, "");

const capitalizeFirstLastInWord = (word: string) =>
    + word.slice(1, -1).toLowerCase()
    + word.charAt(entry.length -1).toUpperCase()

but probably there are better ways to do this :P

EDIT: Ofc some tuning is required if we also accept punctuation and, numbers or any other characters of sort, but the requirements state letters, so...

aminnairi profile image

I think there are a couple mistakes in your solution.

In your capitalizeFirstLastInWord function, you are using the variable entry. I think you meant word?

And in your capitalizeFirstLast function, you are using an undefined variable fixedWord. Maybe this was the return value of your previous function call?

Other than that, I really like how you used the reduce method to solve this one!

tails128 profile image

You are 100% right, I did some last-minute refactoring and I was a bit distracted! :P

I will edit it for future readers :)

About the reduce part: The idea came because last year I was able to work with an
awesome team which enforced very good practices & a very cool "implementation"
of functional programming, I miss that team a lot!