Hey guys i successfully did find the longest word challenge in a different way.
Maybe i am not a programmer brained yet…
here is my solution
function findLongestWord(str) {
var stringToArray = str.split(" ");
var newArray = [];
for(i=0;i<stringToArray.length;i++){
newArray.push(stringToArray[i].length);
}
var sortNumberOfString = newArray.sort(function(a,b){return b-a;});
return sortNumberOfString[0];
}
findLongestWord("The quick brown fox jumped over the lazy dog");
What do you think?
i already checked the original answer with variable set to 0 compared to the length of array element.
i know that, programming wise, approach is very important.
please share your opinion with me on this solutions approach
I would be interested to see a proper bench mark test for this…@P1xt shared a method for doing this I think - I’ll go look it up in a minute and see If I can do a proper test.
My intuition is that creating a whole new array and sorting it probably uses more juice than iterating through stringToArray and comparing [i].lengths to a variable tracking the last known highest length.
I also wonder if Math.max(...newArray) might be a quicker way of getting the max value out of your array rather than the ‘sort n shift’ kind of method you use.
Anyway, I’ll try to find the benchmark test thing…
As for ‘programmer brained’, I think this is definitely evidence of having a programmer brain, since you broke the problem down and used the tools you knew about to effectively come up with a solution.
Your solution is clever, but as @JacksonBates said, it uses more loops and functions so it takes slightly more time to execute. although at this scale with these few words it doesn’t really matter.
Thanks @P1xt. I found it already and have been playing with it.
I’m having trouble getting node to accept (…newArray). I think it’s because ... in this context is a rest parameter instead of spread operator, but running node with a harmony flag (as suggested in the appropriate node issues threads on GH) isn’t working…any ideas?
FWIW, the above suggestion doesn’t beat the suggested solution.
I appreciate you guys sharing your opinion with me
i just learned that ,the more i practice , the more i feel confident
As a newbie, it is tough to understand and solve the problem but i wont give up !!!