-
Notifications
You must be signed in to change notification settings - Fork 0
/
58_LengthOfLastWord.js
47 lines (38 loc) · 1.09 KB
/
58_LengthOfLastWord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const assert = require('assert');
/**
* Given a string s consisting of words and spaces, return the length of the last word in the string.
*
* A word is a maximal substring consisting of non-space characters only.
*
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function(s) {
// Input Check:
if (s == undefined)
{
return 0;
}
// Split the string into an array of strings:
let words = s.split(" ");
// Remove any empty strings that can be produced via
// multiple spaces between words:
const filteredArr = words.filter(function(element)
{
return element !== '';
});
return filteredArr[filteredArr.length - 1].length;
};
describe('58_LengthOfLastWord.js', function ()
{
it('should return 7', function ()
{
let results = lengthOfLastWord(("The last word is longest"));
assert.deepEqual(7, results);
});
it('should return true 5', function ()
{
let results = lengthOfLastWord(("Words are fun and stuff"));
assert.deepEqual(5, results);
});
});