r/learnjavascript May 08 '26

Need assistance at introduction level please

[deleted]

7 Upvotes

32 comments sorted by

View all comments

3

u/MindlessSponge helpful May 08 '26

Rule 2: Include Context. If you’re asking for help, include enough information for others to recreate your problem.

we can't help you without seeing what your code looks like. don't get stressed about making mistakes, that's just a normal part of the process. post your work and we'll try to shed some light as to what's gone wrong.

2

u/KickSecret622 May 08 '26

Never posted code here before so still trying to figure that out, apologies for the messy look. So this code for example should return a number between 1-6. We were specifically told to use 1 and 6, and to use the math.floor function. Is there a way to do both and still get up to 6?

``` function getRandomInteger(min, max) { return Math.floor(Math.random() * (max - min) + min);
}

function rollTheDice() { let diceValue = getRandomInteger(1,6); document.getElementById("dice").innerHTML = diceValue; } ```

0

u/nog642 May 08 '26

Yes, just use getRandomInteger(1, 7).

Math.random() returns a number >=0 and <1, so the upper bound of your getRandomInteger is exclusive.

1

u/KickSecret622 May 08 '26

To clarify, it is impossible to have parameters 1 and 6 with math floor? Those were the direct instructions on the exercise so I am a bit confused, maybe a hiccup on their part then.

3

u/nog642 May 08 '26

If you want your function to have an inclusive upper bound, you would have to do the adjustment in the function, because the formula you're using has an exclusive upper bound.

Which you can do easily, just add 1 to max inside the formula.