Longest Word - DON'T save my ugly code - [SOLVED]

Longest Word - DON'T save my ugly code - [SOLVED]
0

#1

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];

}


#2

I’m gonna get this ! I will, maybe I’ll rewrite everything.


#4

…Ok, now I’m going to spend the next hour comparing what I had in repl/it and what I put in FCC…

Thank you though :slight_smile:


#6

I don’t think I’ll tell you what the mistake was (yes, found it)…
But my, my, my…I’m sooooo proud !!

Thank you P1xt !


#8

thank you @P1xt (yeah, I’m getting the hang of it all, @, [SOLVED], …)