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.

Huangism
16.5k7 gold badges51 silver badges75 bronze badges
asked Jan 14, 2019 at 17:17
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;

};

Ryan Wilson
10.9k3 gold badges23 silver badges46 bronze badges
answered Jan 14, 2019 at 17:20
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
}
answered Jan 14, 2019 at 18:02

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'))

answered Jan 14, 2019 at 17:20

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;
};
answered Jan 14, 2019 at 17:20

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'))

answered Jan 14, 2019 at 17:20

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'));

answered Jan 14, 2019 at 17:21

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
#
answered Jan 15, 2019 at 8:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.