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

114
Visualizações
Function output of JavaScript function not showing in html

The Javascript code is working but I don't know how I can make it work in HTML. I want to link it to an external Javascript file.

 function find_longest_string(input_array) {

  var large = input_array[0].length;     //storing the length of each word in the variable large
  input_array.map(var_new => large = Math.max(large, var_new.length)); //used map to check the largest word
  answer = input_array.filter(var_new => var_new.length == large);    //storing the words which are larger

  return answer[0]; //displaying the first largest word
}

console.log(find_longest_string(['mystery', 'brother', 'aviator','crocodile','pearl','orchard','crackpott']))

HTML code:

<!DOCTYPE html>
<html>
    <body>
    <script src="findLongestWord.js"></script>
    </body>
</html>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I believe you want to print or display the output of that function on the web page.

If that's the case. There are various ways to accomplish this.

Before I go any further, I'd want to point out that you are, in fact, printing something, which is what you intended for your function. But you can't see it since it's printed in the console. You may view this by using the F12 key after loading your website. The console will then appear, and the needed output will be printed.

And here's one method for displaying your output in an HTML page.

You just have to replace your console.log() with the document.write().

 function find_longest_string(input_array) {

  var large = input_array[0].length;     //storing the length of each word in the variable large
  input_array.map(var_new => large = Math.max(large, var_new.length)); //used map to check the largest word
  answer = input_array.filter(var_new => var_new.length == large);    //storing the words which are larger

  return answer[0]; //displaying the first largest word
}

document.write(find_longest_string(['mystery', 'brother', 'aviator','crocodile','pearl','orchard','crackpott']))
<!DOCTYPE html>
<html>
    <body>
    <script src="findLongestWord.js"></script>
    </body>
</html>

Here is another method to achieve this. You can add an html element and change the content of that particular element by using document.getElementById("id name of the element").innerHTML.

Here is the code snippet.

function find_longest_string(input_array) {

  var large = input_array[0].length;     //storing the length of each word in the variable large
  input_array.map(var_new => large = Math.max(large, var_new.length)); //used map to check the largest word
  answer = input_array.filter(var_new => var_new.length == large);    //storing the words which are larger

  return answer[0]; //displaying the first largest word
}
const largest_word = find_longest_string(['mystery', 'brother', 'aviator','crocodile','pearl','orchard','crackpott']);

document.getElementById("demo").innerHTML =largest_word
<!DOCTYPE html>
<html>
    <body>
    <p id="demo"></p>
    <script src="findLongestWord.js"></script>
    </body>
</html>

about 4 years ago · Santiago Trujillo 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