Kosmas Diamantis
Kosmas Diamantis

Reputation: 11

Why for loop stops at first iteration?

I want to find the longest word in the array by using a for loop. But the iteration stops at the first pass. What am I missing?

var a = "hello asdf asdf sdfgfghkkjb";
var s = a.split(" ");
var m = -Infinity;
for (var i = 0; i < s.length; i++) {
  if (s[i].length > m) {
    m = s[i];
  }
}
console.log(m)

Upvotes: 0

Views: 746

Answers (6)

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result, use below sort option with length of each word

  1. Split string by space
  2. Sort arrays of words by length descending
  3. Pick first element for longest word

var a = "hello asdf asdf sdfgfghkkjb";
var s = a.split(" ").sort((a,b) => b.length - a.length)[0]

console.log (s)

codepen - https://codepen.io/nagasai/pen/KYymjd?editors=1010

Option 2: Using reduce method, loop through array once and return longest word

var a = "hello asdf asdf sdfgfghkkjb";
var s = a.split(" ").reduce((acc, v) => {
  acc = v.length > acc.length ? v : acc;
  return acc
}, '')

console.log (s)

codepen - https://codepen.io/nagasai/pen/xePrgj?editors=1010

Upvotes: 2

Daniel McIntyre
Daniel McIntyre

Reputation: 74

You need separate variables to hold the length of the longest string and the current string.

var a = "hello asdf asdf sdfgfghkkjb";
var s = a.split(" ");
var m = -Infinity;
var longest = '';

for (var i = 0; i < s.length; i++) {
  if (s[i].length > m) {
    longest = s[i];
    m = s[i].length;
  }
}
console.log(longest)

Upvotes: 0

Barmar
Barmar

Reputation: 781004

On the iteration, the test is successful, so you do:

m = s[i];

which is equivalent to:

m = "hello";

On the second iteration, you test:

if (s[i].length > m)

which is equivalent to:

if (4 > "hello")

Since you're comparing objects of different types, it converts them to a common type; in this case, it tries to convert the string to a number. Since the string isn't numeric, it's converted to NaN, and any comparison with NaN always returns false.

You need two variables, one for current maximum length, and another for the longest string.

var a = "hello asdf asdf sdfgfghkkjb";
var s = a.split(" ");
var m = -Infinity; // maximum length
var ms; // longest string
for (var i = 0; i < s.length; i++) {
  if (s[i].length > m) {
    ms = s[i];
    m = ms.length;
  }
}
console.log(ms)

Upvotes: 0

jtylerm
jtylerm

Reputation: 472

because the first iteration if(s[i].length > m) has m = -infinity. then you re-assign m's value inside the if block, so the next iteration you're checking if s[i].length is greater than "hello"

Upvotes: 0

mbojko
mbojko

Reputation: 14679

You're comparing words to numbers:

var s = a.split(" ");
var m = -Infinity; // m is a number
for (var i = 0; i < s.length; i++) {
   if (s[i].length > m) { // in first iteration, true, 5 > -Infinity
      m = s[i]; // in first iteration, m becomes a word
   }
}

...in subsequent iterations, you'll have comparisons like "4 > hello".

Upvotes: 0

D Malan
D Malan

Reputation: 11424

In this if-statement you're comparing the length of a word with a word itself: if (s[i].length > m). Change it to compare both of the words' lengths: if (s[i].length > m.length) and set the initial value of m to the shortest "word" possible, e.g. var m = "".

var a = "hello asdf asdf sdfgfghkkjb";
var s = a.split(" ");
var m = "";
for (var i = 0; i < s.length; i++) {
  if (s[i].length > m.length) {
    m = s[i];
  }
}
console.log(m)

Upvotes: 4

Related Questions