dimanche 9 juin 2019

Why doesn't my code return the word with the most characters? [duplicate]

This question already has an answer here:

I am trying to get JS to get the word with the most characters but for some reason my code always returns "The"

I have tried two variants of the code.

Variant 1:

let sent = "The quick borwn fox jumped over the jazy dog";
let word = findWord(sent);

function findWord(sent){

  splitSent=sent.split(" ");

  let largest="";

  for(i=0; i<splitSent.length;i++){

    if(splitSent[i].length>largest){

        largest=splitSent[i];
    }

  }

return largest;
}

console.log(word)

Variant 2:

let sent = "The quick borwn fox jumped over the jazy dog";
let word = findWord(sent);

function findWord(sent){

  splitSent=sent.split(" ");

  let largest="";

  for(split of splitSent){

    if(splitSent[i].length>largest){

        largest=splitSent[i];
    }

  }

return largest;

}

console.log(word)

the console just prints "The" from the first code and "fox" from the second

Aucun commentaire:

Enregistrer un commentaire