0

I have some troubles with the console's output when I try to run some snippet js code from the "source" tab. Instead of getting a valid output, I get "undefined" (I've checked the code in JSFiddle and it's correct the output is:20 )

Why does it happen and how can I resolve it?

JS:

    //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);

enter image description here enter image description here

yakov sachuk
  • 145
  • 1
  • 11
  • You are on the issues tab instead of the console tab. – CherryDT Jul 02 '22 at 21:38
  • Yeah I know I Hoped maybe it would give some additional info – yakov sachuk Jul 02 '22 at 21:40
  • Where do you see `undefined`? I don't see it in your screenshot. – Ivar Jul 02 '22 at 21:42
  • I see it in the console tab – yakov sachuk Jul 02 '22 at 21:43
  • @ivar, just create a snippet with provided code and test it yourself. I'm getting the same `undefined` pointed on 2nd line of the code, which is a comment... *correction, after deleting the snippet and creating a new one, now the `undefined` pointed on first line, which still a comment. – vanowm Jul 02 '22 at 21:45
  • 3
    You are filtering on a search term ("offsetParent"). Remove that. – Ivar Jul 02 '22 at 21:45
  • The `undefined` seems to be pointing into `VM504 Script snippet #3:1`, while the scripts output comes from `Script snippet #3:19 ` and in the `VM504` the source shows an extra tab character at the beginning of first line before the comment – vanowm Jul 02 '22 at 21:49
  • I cannot reproduce that @vanowm. Got a completely empty HTML file except for that script and it just logs 20 fine. (Same in the Fiddle, but OP already mentioned that that works.) – Ivar Jul 02 '22 at 21:50
  • well, I opened devtools on this website, and created new snippet right there...no other snippets exist. In fact just creating an empty snippet and pressing CTRL+Enter to run it, shows `undefined` in the console. – vanowm Jul 02 '22 at 21:51
  • Nice to know that's a thing in Chrome. [But it still shows 20 for me nevertheless](https://i.stack.imgur.com/gjBgz.png). Make sure the it shows all log levels. (For me, the Info was unchecked while I'm pretty confident that it was checked before, so maybe an update reset it.) I'm pretty confident the `undefined` is just the regular REPL "log the outcome of the last statement" behavior, which is the return value of the `console.log()`. – Ivar Jul 02 '22 at 21:55

1 Answers1

1

Snippets are code run directly in the console, it output the returned value of last statement, in your case it's console.log(), which doesn't return anything, hens it shows undefined. (it's like typing in console: console.log(console.log("ok")) )

If your remove console.log() all together, it will print out the value of the variable instead:

    //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;

You can get rid of bmi variable too, just execute the function.

vanowm
  • 9,466
  • 2
  • 21
  • 37