Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Get the longest word from sentence algorithm problem with javascript

I am building an algorithm of my own that is supposed to return the longest word from a sentence with javascript. The custom algorithm is compiling but returning an incorrect string. Am trying to solve programming problems without plagiarising so please help....

logic

a) Form an array out of the sentence using .split(" ") function.

b) Define a variable index that will be used to store the index of the element with the largest length

c) Define a default string mystring that we will use for comparisons in the loop

d) Iterate through the array we formed in step while comparing each string at each index with our variable mystring, if the string at the specified position is larger than our mystring,we get the index of that string and store it in the index variable..

e) We return the string at the index of the array that we got from the loop

Expectations

For a sample sentence like I love the United States of America,the algorithm should return America as the longest string. Instead its returning United which is false. The rest of the problem is commented in my code below

Code

function longestWord(sentence) {
  //we use this index to store the index of the 
  //longest word in the array
  let index = 0;
  //define an empty string for comparison
  let mystring = "";
  //define a new array
  let arr = sentence.split(" ");
  //iterate through the array
  for (let p = 0; p < arr.length; p++) {
    if (arr[p].length > mystring.length) {
      //get the index and store in the variable
      index = p;

    }
  }
  //return the element at the index we got
  return arr[index];
}
console.log(longestWord("I love the United States of America"));
//function returns United, please help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you forgot to change the mystring variable valut to the new string value.

try this :

      function longestWord(sentence) {
       //we use this index to store the index of the 
       //longest word in the array
       let index = 0;
       //define an empty string for comparison
       let mystring = "";
       //define a new array
      let arr = sentence.split(" ");
      //iterate through the array
      for (let p = 0; p < arr.length; p++) {
      if (arr[p].length > mystring.length) {
        //get the index and store in the variable
        index = p;
        mystring=arr[index];

      }
   }
     //return the element at the index we got
     return arr[index];
  }
  console.log(longestWord("I love the United States of America"));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda