A question about functions

Tell us what’s happening:
Describe your issue in detail here.
Hello, everyone!
I needed to create a function that makes a random digit in your given range and the problem is that when I declare it in the function and console.log the number it gives me numbers that are even bigger than myMax. If I declare the variable outside of the function and console.log it outside the function everything is okay. So I am really interested in why does it behave like that?

  **Your code so far**

function randomRange(myMin, myMax) {
// Only change code below this line
var number;
number = Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
return number;
// Only change code above this line
  **Your browser information:**

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

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

Hi, I could not replicate the issue, I replaced your console.log with:

console.log(`myMin:${myMin}, myMax:${myMax},  RANDOM:${number}`);

and the random number never exceeded the max.

You may be having an issue with global vs local declaration. Show us the exact code of the two variations so we know exactly where you’re declaring and where you’re console logging.

I could reproduce the error when checking the code with this challenge, and it’s very confusing to me what I see in the console. There seem to be multiple function calls even when I don’t run the tests and only console.log one function call. And yes, the logs showed numbers higher than myMax, but the challenge still passed.

1 Like

The test runs a loop and checks the return (which are the numbers you see logged in the output).


var calcMin = 100;
var calcMax = -100;
for(var i = 0; i < 100; i++) {
  var result = randomRange(5,15);
  calcMin = Math.min(calcMin, result);
  calcMax = Math.max(calcMax, result);
  if(typeof myRandom === 'number') {
    return "myRandom = " + myRandom;
  } else {
    return "myRandom undefined";

Not sure why, but after resetting the challenge the code you posted passes. It didn’t pass at first. But now after the reset, I can’t seem to make it fail again.

1 Like

That sort of explains why I see 100 logs when I open the challenge, and why they’re outside the range of my function call (1,6). Not sure if I’m supposed to see those tests running before I even clicked on “Run the Tests”.

I’m also not sure if it’s intended that the challenge passes for this:

number = Math.floor(Math.random() * (myMax - myMin + 1) + myMin);

… when it should actually be (note parenthesis):

number = Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;

I think it doesn’t make a difference for these ranges though.

You wouldn’t see them if the code posted didn’t have a console.log statement.

Ok that’s surprising, I thought those tests only run after a click on “Run the tests”, not every time I type something. Thanks for clarifying.

I remember the code blocks used to be called setup and teardown. Found some old code in an issue that shows it.

But I’m not sure if the names were an actual reflection of the execution order. I seem to remember there being something weird about it. Not sure if we just have one block now or two. I’m sure there is a PR somewhere that has the changes but I haven’t really looked for it.

It does not make a difference i played around with that because the formula in the task is written in one way but in the solutions, they write it another way. I think the main issue is a console.log and a new var in the function because when I delete them or take them out of the function it works fine. So I was really interested why does it behave like this.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.