xxxxxxxxxx
// length of last word leetcode solution javascript
var lengthOfLastWord = function(s) {
// convert string into an array and split by spaces
let arr = s.split(" ");
// store array length value
let length = arr.length;
// loop through array length
for(let i=0; i<length; i++){
// check if last element is empty, if it is empty then remove last element using arr.pop()
if(arr[arr.length - 1] === ""){
arr.pop();
}
// check if last element is not empty, if it is not empty then break the loop
if(arr[arr.length - 1] !== ""){
break;
}
}
// return last element length
return arr[arr.length - 1].length;
};