The function returns Undefined. What’s wrong?

Asked

Viewed 69 times

1

var pulaLinha = function() {
  document.write("<br>");
}

var mostra = function(frase) {
  document.write(frase);
  pulaLinha();
}

var calculaIMC = function(altura, peso) {
  var imc = peso / (altura * altura);
  return;
}

var alturaDoUsuario = prompt("Sua altura? ");
var pesoDoUsuario = prompt("Seu peso? ");

var imcDoUsuario = calculaIMC(alturaDoUsuario, pesoDoUsuario);
mostra("O seu imc é: " + imcDoUsuario);

  • 3

    And this return;?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

4

Of course, the code is returning nothing. The function makes a calculation in there and abandons this resulting. A return returns nothing, um return <expressão> returns the expression.

function calculaIMC(altura, peso) { return peso / (altura * altura); }

console.log("O seu imc é: " + calculaIMC(prompt("Sua altura? "), prompt("Seu peso? ")));

I put in the Github for future reference.

  • Ok! Thank you very much. It worked perfectly.

  • 1

    I think it would also be interesting to mention that the prompts and consequential variables alturaDoUsuario and pesoDoUsuario in the question code are strings and only works properly due to implicit conversions that are made.

  • @Isac gets his comment as this warning :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.