--DAY 11--
Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
-Problem: Length of Last Word
-Detail: here
-Idea: I will use string string method in javascript to solve it
+) Use split()
to split string into array
+) trim()
to remove space of the last word
+) Return it's length
->So simple, right?
-My solution (javascript):
var lengthOfLastWord = function(s) {
s=s.trim().split(' ');
return s[s.length-1].trim().length;
};
-->If you have better solution or any question, please comment below. I will appreciate.
Top comments (4)
// there is no need for the second trim
var lengthOfLastWord = function(s) {
s = s.trim().split(' ');
return (s[s.length-1].length);
};
oh, I didn't notice that. Thanks !