Created
April 12, 2018 21:08
-
-
Save y0n4/3b4b41bbb031237e0753f6aa9e95baea to your computer and use it in GitHub Desktop.
58 length of last word (yona)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//prompt >> count the length of last word | |
//the input >> string, output >> number | |
//if no last word, there should just be a 0 | |
var lengthOfLastWord = function(s) { | |
//if argument is not being passed as a string | |
if (typeof s !== 'string') { | |
return 0; | |
} | |
//get the input to be divided individually so we can identify each element | |
var words = s.split(' '); | |
//remove any possible empty strings/spaces left behind | |
if (words.includes('')){ | |
words = words.filter(element => element !== ''); | |
} | |
//if there are words or characters left in our updated array | |
if (!words.length){ | |
return 0; | |
} | |
//basic goal is to return the length of the last element | |
return words[words.length -1].length; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment