Find the Longest word in a string i made it in a bit different way [Spoiler]

Find the Longest word in a string i made it in a bit different way [Spoiler]
0

#1

Hey guys i successfully did find the longest word challenge in a different way.

Maybe i am not a programmer brained yet…

here is my solution :slight_smile:

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

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

What do you think?
i already checked the original answer with variable set to 0 compared to the length of array element.

i know that, programming wise, approach is very important.
please share your opinion with me on this solutions approach

Thanks


#2

I would be interested to see a proper bench mark test for this…@P1xt shared a method for doing this I think - I’ll go look it up in a minute and see If I can do a proper test.

My intuition is that creating a whole new array and sorting it probably uses more juice than iterating through stringToArray and comparing [i].lengths to a variable tracking the last known highest length.

I also wonder if Math.max(...newArray) might be a quicker way of getting the max value out of your array rather than the ‘sort n shift’ kind of method you use.

Anyway, I’ll try to find the benchmark test thing…

As for ‘programmer brained’, I think this is definitely evidence of having a programmer brain, since you broke the problem down and used the tools you knew about to effectively come up with a solution.

Good job :slight_smile:


#3

Your solution is clever, but as @JacksonBates said, it uses more loops and functions so it takes slightly more time to execute. although at this scale with these few words it doesn’t really matter.


#5

Thanks @P1xt. I found it already and have been playing with it.

I’m having trouble getting node to accept (…newArray). I think it’s because ... in this context is a rest parameter instead of spread operator, but running node with a harmony flag (as suggested in the appropriate node issues threads on GH) isn’t working…any ideas?

FWIW, the above suggestion doesn’t beat the suggested solution.


#6
benchmark-testing ➤ node longestWord.js                                  
lkh312 x 587,396 ops/sec ±2.87% (52 runs sampled)
suggested x 2,945,138 ops/sec ±2.67% (51 runs sampled)
Fastest is suggested

So the suggested solution is roughly x5 faster!

But you still solved an algorithm with a readable solution, which is what really counts.


#8

Well I feel sheepish…

v4.2.6

I totally should’ve checked that! :blush:


#9

Ok, last one:

lkh312 x 845,313 ops/sec ±2.26% (79 runs sampled)
mathMax x 453,402 ops/sec ±0.71% (85 runs sampled)
suggested x 5,037,542 ops/sec ±0.70% (90 runs sampled)
P1xt x 931,357 ops/sec ±0.88% (86 runs sampled)
Fastest is suggested

So @P1xt’s one-liner is still slow, but it’s a one-liner so it wins by default :slight_smile:

Also, Node 6 is x2 faster than Node 4.

Testing Script I used (expand)
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var str = 'The quick brown fox jumped over the lazy dog';


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

function findLongestWordMathMax(str) {
  var stringToArray = str.split(" ");
  var newArray = [];
  for(i=0;i<stringToArray.length;i++){
    newArray.push(stringToArray[i].length);
  }
  return Math.max(...newArray);
}

function findLongestWordSuggested(str) {
  var words = str.split(' ');
  var maxLength = 0;

  for (var i = 0; i < words.length; i++) {
    if (words[i].length > maxLength) {
      maxLength = words[i].length;
    }
  }

  return maxLength;
}

function findLongestWordP1xt(str) {
    return str.split(' ').reduce((x, y) => Math.max(x, y.length), 0);
}

// add tests
suite.add('lkh312', function() {
  findLongestWordlkh312(str)
})
.add('mathMax', function() {
  findLongestWordMathMax(str)
})
.add('suggested', function() {
  findLongestWordSuggested(str)
})
.add('P1xt', function() {
  findLongestWordP1xt(str);
})
// add listeners
.on('cycle', function(event) {
    console.log(String(event.target));
})
.on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });

EDIT
Doh, I noticed a stoopid bug…
Update
Ok, I fixed my stoopid bug and updated the benchmark report.


#11

I appreciate you guys sharing your opinion with me
i just learned that ,the more i practice , the more i feel confident
As a newbie, it is tough to understand and solve the problem but i wont give up !!!

Happy coding


#13

Hello, I have moved your post to the right category int he forum as this is not a wiki entry.