It’s embarrassing, for two reasons:
-
My code is terribly contrived (- why, but why do I do that?)
-
I can sort of feel where it goes wrong but I can’t fix it (yes, I still want to make my ugly code work, because you see it’s all my own, and I used to have panic attacks upon seeing a mere + sign).
I get the right answer but can’t call the function twice. So there’s something in there that is stuck - but all my variables are declared inside the function.
Again, I’m this close to going for solutions seen out there - I understand them, but then I always seem to understand when I read, it’s another thing when I do it myself. That’s why I want to save my ugly code, and then one day I’ll come back and try the challenges again more efficiently.
function sortNumber(a,b) {
return a - b;}
function findLongestWord(str){
var mySplit = str.split(" ");
var newArray = [];
for (var i=0; i<mySplit.length; i++){
newArray.push(mySplit[i].length);
}
console.log(newArray);
newArray.sort(sortNumber);
newArray.reverse();
var result = newArray.splice(0,1);
console.log(result);
return result[0];
}