LeetCode #58: Length of Last Word

Photo by Joshua Hoehne on Unsplash
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
var lengthOfLastWord = function(s) {
const words = s.trim()
}
var lengthOfLastWord = function(s) {
const words = s.trim().split(' ');
}
var lengthOfLastWord = function(s) {
const words = s.trim().split(' ');
const lastWord = words.pop
}
var lengthOfLastWord = function(s) {
const words = s.trim().split(' ');
const lastWord = words.pop || ' ';
return lastWord.length;
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melissa Guachun

Software Developer and visual artist based in NYC. Join me on my journey to coding enlightenment or a torrential mental breakdown, whichever comes first.