Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaff3cea

Browse files
authored
Update 58-Length-Of-Last-Word.js
Added another approach for length-of-last-word in JS.
1 parent82e9776 commitaff3cea

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎javascript/58-Length-Of-Last-Word.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,21 @@
1313
}
1414
returnlen;
1515
};
16+
17+
// another approach. starting out from the last so we don't have to go all the way to the end.
18+
varlengthOfLastWord=function(s){
19+
20+
letfirstCharOccurance=false;
21+
letlastWordLen=0;
22+
23+
for(leti=s.length-1;i>-1;i--){
24+
if(s[i]!==' '){
25+
firstCharOccurance=true;
26+
lastWordLen++;
27+
}
28+
if(firstCharOccurance&&s[i]===' '){
29+
break;
30+
}
31+
}
32+
returnlastWordLen;
33+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp