var arr =[];
function findLongestWord(str) {
str = str.split(’’).join(’’); //Splits the string, makes array
console.log(str);
for(var i=0; i<str.length; i++) { // for every whitespace index is stored in arr.
if (str[i].indexOf(’ ') >= 0){
if (arr.length === null) {
arr.push(i);
}
else {
arr.push(i); // the problem is here
console.log(arr);
}
}
}
// return Math.max.apply(Math, arr);
}
findLongestWord(“The quick brown fox jumped over the lazy dog”);
The values arr has is [3, 9, 15, 19, 26, 31, 35, 40]. Is there any way I can access the array value that is before the one I’m at. For example: When i=9 I want to access value 3 and push it in arr at the else statement.
Yeah, the join was unnecessary. I made the change and split at spaces. I saw the solution you are suggesting of. I was just wondering if there is a way to access the value at a specific index while looping.
Sorry, I missed the fact you were working with two arrays. It seems to me that you are particularly interested in the last element of the arr array. If that’s so then you can use the length of arr and subtract one from it to access the last element.