Alright, so basically this code passes in a sentence into the function and the function needs to figure out which word is longest to return. Everything works great except that the very last letter keeps getting cut off. So what would be a good solution to this problem?
function LongestWord(sen) {
sen = sen.toLowerCase();
var build = "";
var arr = [];
var longest = 0;
for(var i = 0; i < sen.length;i++){
var cur = sen.charCodeAt(i);
console.log(sen.charCodeAt(i))
if(i == sen.length - 1){
arr.push(build);
}
if(sen.charAt(i) === " "){
arr.push(build);
build = "";
}
if(cur >= 97 && cur <= 122){
build += String.fromCharCode(cur);
}
}
console.log(arr);
for(var e = 0; e < arr.length - 1;e++){
if(arr[e].length > arr[e + 1].length){
longest = arr[e];
}
else{
longest = arr[e + 1];
}
}
return longest;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
console.log(LongestWord("Johnny ErsoL"));
It returns "Johnny", which is correct, but this is what the Array looks like at the end.
[ 'johnny', 'erso' ]