What is happening here with this loop and its associated psuedocode and how can I avoid associated errors?

What is happening here with this loop and its associated psuedocode and how can I avoid associated errors?
0

#1

Please see below:

var mySentence = 'the quick brown fox jumps over the hound';
function longestWord (sentenceAnalyze1) {
 
int index = 0; 
int elementLength = array[0].length();
for(int i=1; i< array.length(); i++) {
    if(array[i].length() > elementLength) {
        index = i; elementLength = array[i].length();
    }
}
return array[index];

I wanted to make sure my understanding is correct for the following situations:

  1. The initial condition starts at 1 and it goes the length of the array, by one index at a time.
  2. If the length of the array is greater than the length of the index
  3. Then something happens - what is it that happens

Note: I believe I need to define a variable at the top and set it to zero? Is that correct?
The code is currently not in javascript but it seems very similar.

Here is the full link: https://stackoverflow.com/questions/26725296/finding-the-longest-string-in-an-array-of-strings

note 2: there are two words that could be considered the longest word but not both of them are at the end of the sentence.


#2

I’m leaving right now but I will be back in a couple of minutes.


#3

The link you posted is to Java code, not javascript. int isn’t a javascript keyword. The sentence has four 5 letter words, do you want to find the first, the last or all of them? array is’t defined. Your function isn’t closed with a }. Your function isn’t called anywhere in this code.

    if(array[i].length() > elementLength) {
        index = i; elementLength = array[i].length();
    }

This bit is checking whether the length of the i-th element is greater than the longest previous word and, if it is, updating the stored length and the index at which it was found.

In pseudocode, you’d write something like

  1. split the sentence into words on spaces, creating an array of words
  2. loop through the words in the array and
  3. if the word is the longest one we’ve seen, store its index and length

#4

var mySentence = ‘the quick brown fox jumps over the hound’;
///update: this is supposed to be running in the background ( I am practicing for a test later on so the assumption is that the parameter of this function is already connected to the above; I dont need to know how; it just is!

function longestWord (sentenceAnalyze1) {

int index = 0;
ArrayNow =sentenceAnalyze.split
for(i=1; i< array.length(); i++) {
if(ArrayNow[i].length() > elementLength) {
index = i; elementLength = array[i].length();
}
}
return array[index];

Again my psuedo code is:
split the sentence
loop through the array of words
if the array index (for example index 3 in this case) is long it stores it
a) How does it account for if it chooses the longest word and it is at the end of the sentence?
b)Is that what element length = array[i].length is about?
c) Also to display the longest word don’t I need to store the longest word in it’s own array at the end?


#5

Am I even on the right track? It’s not just about getting the correct answer. It’s about if I can use my logical thinking skills, which I think I’m getting better at.


#6

I think this got off topic and that was a fault on my side.


#7

Beyond the syntactic issues, yes, this is a pretty common pattern for collecting the largest value from a collection.

You might want to look into reduce as well, which can express this same idea in simpler terms.