Longest Word in a String

Hi all any advice on how i can make below work?

function findLongestWord(str) {
	var newArray = str.split(" ");
	var lengthArrayObjects = [];
	var arrayLength = length.newArray;
	for(i=0; i<arrayLength; i++) {
        lengthArrayObjects += lengthArrayObjects.push(newArray[i]);
	lengthArrayObjects.sort((function(a, b){return b-a}));

	return lengthArrayObjects[0];

findLongestWord("The quick brown fox jumped over the lazy dog");

First fix the syntax errors (at least two).

Then rename all your variables, as you’re confusing both yourself and us readers.

Then read it slowly again and imagine running each line separately.

Thank you. I’ve gone back and cleaned a bit of my code .

Can you describe what you think the following line is doing?

lengthArrayObjects += lengthArrayObjects.push(newArray[i]);

I’m thinking this is creating an item in the array for each iteration of i.


Actually, your for loop does not execute, because of the following line:

var arrayLength = length.newArray;

To help you debug your code, you can use console.log statements at various points in your code to check the values of variables. Put console.log(arrayLength); after the above line and then look at your browser’s console (Ctrl+Shft+J in Chrome) to see what gets displayed. I think you will be surprised.