0
function LongestWord(sen) { 
    let arr = sen.split(' ');
    let longestWord;
    for (let i = 0; i < arr.length; i++) {
        let counter = 0;
        if (arr[i].length > counter) {
            counter = arr[i].length;
            longestWord = arr[i];
        }
    }
    return longestWord;
};

The objective of the function is to cycle through an array and find the longest word. I've been looking through this and everything seems correct, though, obviously something is wrong but I am not seeing it.

2
  • 3
    You keep resetting the counter to 0 with each loop iteration. Set counter to 0 before the for loop. Commented Jan 14, 2019 at 17:20
  • 3
    The most important thing I learned being a dev is to use debugger and stepping thru code. developers.google.com/web/tools/chrome-devtools/javascript Commented Jan 14, 2019 at 17:21

7 Answers 7

2

It is because you set count to zero on every iteration, i.e. rewrite to

function LongestWord(sen) { 
let arr = sen.split(' ');
let longestWord;

let counter = 0;  // Moved here!!!

for (let i = 0; i < arr.length; i++) {
    if (arr[i].length > counter) {
        counter = arr[i].length;
        longestWord = arr[i];
    }
}
return longestWord;

};

Sign up to request clarification or add additional context in comments.

Comments

2

I know this is a whole different approach but you can achieve same result using this more concise code, and without needing to apply any loop structure:

function LongestWord(sen) { 
  let arr = sen.split(' ');
  return arr.sort(function(a, b){
    // Sort Descending
    return b.length - a.length;
  })[0]; // Take first and longest element
}

Comments

1

You need to use counter out of for loop. Because as you're using in for loop every time it is getting re-initialized.

function LongestWord(sen) { 
    let arr = sen.split(' ');
    let longestWord;
    let counter = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].length > counter) {
            counter = arr[i].length;
            longestWord = arr[i];
        }
    }
    return longestWord;
};

console.log(LongestWord('heya 1 2'))

Comments

1

You are redeclaring counter with every iteration.

function LongestWord(sen) { 
    let arr = sen.split(' ');
    let longestWord;
    let counter = 0;

    for (let i = 0; i < arr.length; i++) {
        if (arr[i].length > counter) {
            counter = arr[i].length;
            longestWord = arr[i];
        }
    }
    return longestWord;
};

Comments

1

You need to define counter outside of the loo, because you need it for keeping the length

function longestWord(sen) { 
    let arr = sen.split(' ');
    let longestWord;
    let counter = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].length > counter) {
            counter = arr[i].length;
            longestWord = arr[i];
        }
    }
    return longestWord;
}

console.log(longestWord('orange banana potatoe hackfleischbällchen rice'))

Comments

1

function LongestWord(sen) { 
    let arr = sen.split(' ');
    let longestWord;
    let counter = 0; // you need to place counter outside for loop
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].length > counter) {
            counter = arr[i].length;
            longestWord = arr[i];
        }
    }
    return longestWord;
};

console.log(LongestWord('something is wrong with this'));

Comments

0

How about something like this

# cat longest_word.js
function longestWord(str) {
        return str.split(' ').reduce(function (acc, word) {
                return acc.length > word.length ? acc : word;
        });
}

console.log(longestWord('the quick brown fox jumps over the lazy dog'));
# node longest_word.js
jumps
#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.