Uppercase the first character
Write a function
ucFirst(str) that returns the string
str with the uppercased first character, for instance:
ucFirst("john") == "John";
But we can make a new string based on the existing one, with the uppercased first character:
let newStr = str.toUpperCase() + str.slice(1);
There’s a small problem though. If
str is empty, then
undefined, and as
undefined doesn’t have the
toUpperCase() method, we’ll get an error.
There are two variants here:
str.charAt(0), as it always returns a string (maybe empty).
- Add a test for an empty string.
Here’s the 2nd variant: