function findLongestWordLength(str) {
let arrSplit = str.split(" ");
let arsl = arrSplit.length;
let index1 = 0;
let index2 = 1;
function recurse() {
if (arsl == 1) {
return arrSplit[index1];
}
if (arsl > 1) {
if (index1.length > index2.length) {
arrSplit.splice(index2);
}
if (index1.length < index2.length) {
arrSplit.splice(index1);
}
if (arsl > 1) {
recurse()
}
}
}
return arrSplit[index1];
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
function findLongestWordLength(str) {
let arrSplit = str.split(" ");
let arsl = arrSplit.length;
let index1 = 0;
let index2 = 1;
function recurse() {
if (arsl == 1) {
return arrSplit[index1];
}
if (arsl > 1) {
if (index1.length > index2.length) {
arrSplit.splice(index2);
}
if (index1.length < index2.length) {
arrSplit.splice(index1);
}
if (arsl > 1) {
recurse()
}
}
}
return arrSplit[index1];
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13597.66.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.109 Safari/537.36.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
I didn’t even realize that I never actually called the recurse function ! Lol. Thank you for your response!
I was thinking that if I compared the length of every word, storing the word with the largest length that would make the most sense, however I couldn’t really wrap my head around how I would actually go about doing that.
So eliminating the indices in an array created from the string seemed logical to me.
The function should return the length of the longest word in the sentence, so you don’t need to store the word itself, just the length. You have the right idea though, go through every word in the sentence and keep track of the longest word length you find. You are on the right track by splitting the string into an array. I bet you know how to go through each item in an array, right? You just have to keep track of the longest word you find in the array and return it. I wonder if JS provides a mechanism for storing a number (the length of a word) and updating that number if you find an even longer word?