Tengo algunos problemas con la salida de la consola cuando intento ejecutar un fragmento de código js desde la pestaña "fuente". En lugar de obtener una salida válida, obtengo "indefinido" ( revisé el código en JSFiddle y es correcto, la salida es: 20 )
¿Por qué sucede y cómo puedo solucionarlo?
J.S .:
//Create your function below this line. //The first parameter should be the weight and the second should be the height. function bmiCalculator(weight, height){ return Math.round(weight/Math.pow(height,2)); } /* If my weight is 65Kg and my height is 1.8m, I should be able to call your function like this: var bmi = bmiCalculator(65, 1.8); bmi should equal 20 when it's rounded to the nearest whole number. */ var bmi = bmiCalculator(65, 1.8); console.log(bmi);Los fragmentos son código que se ejecuta directamente en la consola, genera el valor devuelto de la última declaración, en su caso es console.log() , que no devuelve nada, cuando muestra undefined . (es como escribir en la consola: console.log(console.log("ok")) )
Si elimina todos juntos console.log() , imprimirá el valor de la variable en su lugar:
//Create your function below this line. //The first parameter should be the weight and the second should be the height. function bmiCalculator(weight, height){ return Math.round(weight/Math.pow(height,2)); } /* If my weight is 65Kg and my height is 1.8m, I should be able to call your function like this: var bmi = bmiCalculator(65, 1.8); bmi should equal 20 when it's rounded to the nearest whole number. */ var bmi = bmiCalculator(65, 1.8); bmi; También puede deshacerse de la variable bmi , simplemente ejecute la función.