JavaScript: Length of last word

Last update: 14 Sep 00:02
4
Students
100%
Completion rate

solution.js

Write and export as default a function returns the length of the last word of an input string. Word is any sequence of characters without gaps (spaces).

import getLastWordLength from './solution.js';

getLastWordLength(''); // 0

getLastWordLength('man in BlacK'); // 5

getLastWordLength('hello, world!  '); // 6

Tips

  • To remove extra spaces use trim method

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests