Random Whole Numbers with JavaScript - problem with executing

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**
function randomWholeNum() {

// Only change code below this line
var randomWholeNum = Math.floor(Math.random() * 10);
console.log(randomWholeNum);

}

Hello everyone. I have problem with executing result. In console i have whole number but when I try to finish task there is a message:
" The result of

randomWholeNum

should be a whole number."

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Generate Random Whole Numbers with JavaScript

Link to the challenge:

You don’t return anything from your function.

Note - you shouldn’t use var. Use let or const.

1 Like

It passes with last added " return randomWholeNum; "?

No, your function should use the return keyword to return a value. Right now you’re logging a value to the console, then returning undefined implicitly.

1 Like

Thanks @JeremyLT and @colinthornton . Can someone explain why var dont work in this solution?

var works but for all intents and purposes it’s a legacy feature that only exists for backwards compatibility with programs that were written when it was the only choice. In modern code you should avoid using var because it has some strange behaviors that can be difficult to debug when you run into them (in this specific case it didn’t matter).

So basically, don’t use var. Use const by default, use let when you need it.

1 Like