How to understand the code?

I have tasks, but I don’t understand anything there at all, even when I look at the answer, I don’t understand at all how to write the code in js, that’s the question

  1. Generate a new 4 digit random number.
  • Range for random number 1000 - 9999.
    1. Make sure this new random number doesn’t match any of the numbers in the myNumbers array.
    1. If it matches, you need to generate a new 4-digit number.
    1. If there are no matches (the new number is unique), add it to the myNumbers array.
  • IMPORTANT: In solving the problem, use the following functions:
    • to generate a random number in a given range
    • to add a random number to an array and return the modified array
      I started writing code, but I don’t understand why my Rondom doesn’t work? there are also a lot of questions, in the answer to this task I don’t know the code at all.
const MIN = 1000
const MAX = 9999

const myNumbers = [2355, 7235, 8135, 1762, 2361, 8351]

const mat =Math.floor(Math.random(MIN, MAX))

function mtr(){
    if(mat !== myNumbers) {


Who’s giving you the tasks?

If this is a homework assignment, and you dont understand anything here, you need to go to your instructor and tell them that; there’s a fundamental problem with how you’re being taught/learning.

An instructor would/should not be giving an assignment that you are incapable of understanding.

This is not homework, I am studying myself, this is the 36th task, but I understood everything about the 35th task, but here I don’t know anything, more precisely, my solution does not work, and in a hurry the teachers, when I was studying, gave difficult tasks , they were solved by the whole group for 2 hours and this is normal, that’s why I stopped studying, because the teacher should be able to teach students, there are no such teachers in Russia.

Okay… well, if you understood the first 35, hopefully this will make sense to you as a starting point…

Math.random does not take parameters directly. It returns a floating point number between 0 and 1.

The MDN page for Math.random() highlights how you can get a number between min and max using Math.random and some math.

I read this, but I see the same thing in the answer to the problem, but I don’t understand such a record, I start to get confused in such solutions, everything is somehow complicated and there is a lot of code that is not clear.

If there’s a lot of code that is difficult, treat it the same way you would eat an elephant - one bite at a time.

Start with Math.random and figure that out. Then move on to the next method.


So you read the section about “Getting a random integer between two values”. What part of that was not clear?

It’s 4 lines of code, 2 of which you can ignore. There’s less characters in that code block than there are in this post.

  • (max - min) + min); this is part of the code, I don’t understand why subtract then multiply or add why is all this necessary?
    it turns out that two numbers in the arguments get the same value, then it is multiplied by a random number and after that the minimum number that was at the input is added to this number, right?

I am sorry, but this is absolut basic mathematics. If you do not understand this, you might learn mathematics first as you will need it anywhere in software development.

Right, so random returns a number between zero and one.

So let’s say I want a number between 0 and 200, to keep it simple.

So we know that 0 * 200 is 0 and 1 * 200 is 200.

So to get a number between 0 and 200 I can multiply random() with 200.

Now, let’s say I want a number between 100 and 300. What I can do here is the same as before, but add 100 at the end, so 0 to 200 shifts to 100 to 300.

Work this out to become generic and you’ll indeed get

min + Math.random() * (max - min)

1 Like

clear, but I’m good at math

I still don’t understand how exactly to write it all down, and why such difficulties in elementary things, really didn’t they come up with such a property so that you can simply set the range from and to? it’s simple, but you sit here all day and don’t understand why these multiplications and how it works, if it randomly gives out a lot of digits after the decimal point, and this is also a multiplication, then subtraction or addition I don’t understand.

Agreed it would it have been a lot simpler if Javascript just had a function that could return a random number between some min and max you provide. It does not, so we have to work with what we’ve got.

As for how it works, repeating you don’t understand won’t help you much. Grab some pen and paper and work through some examples.

1 Like

No, I think it’s pointless to understand nonsense) this is not a big improvement and there are many similar moments, a person should just understand such materials, but it’s impossible to understand here, I just have to agree that this is how it should be)))

I find that the best way for me to understand the random function is that it gives a random number between 0 and 1 (might be 0, can never be 1 itself) such as 0.3686063272429352 or 0.9701173049920733

That way you can multiply it by anything you want, to get a random number from 0 up to your max value.

Want a number from 0 to 200? Then multiply the random number by 200.

Don’t want the 0? Then add 1 to get a number from 1 to 201.

Prefer 200 as the upper bound instead of 201? Then multiply by one less, 199 instead of 200, before adding 1.

If those are too many numbers for you then I’m sorry. Welcome to computing where computers are all numbers. You can try to hide those numbers, but you can’t escape them.


Well, somehow you can understand, but everything is definitely superfluous, when a beginner does not understand and there are a lot of them, I need to facilitate students, but here it is imposed and this is bad, I think that in a couple of years this logic will be replaced by non-roaming networks, where projects will be done not in months, but in 10 minutes, and this will happen anyway, otherwise it is impossible to perceive such complex codes.

Even if I am going totally of topic, I find it an interesting discussion.

In the last thousands of years, the human becomes more and more intelligent. 1000 years ago no one thought, that one time nearly all human can calculate 3*3 = 9

In the last 100 years we become much more intelligent and get so much more knowledge that we are able to develop such complex software which should now overtake?

So in another 100 years human will become back dump again and not able to calculate 3*3=9 because a KI is doing all this for us?

Looks like the new generation has no afford to learn something by themselves but want to get everything presented by a keyboard hit?

Not quite and not in one click, for example, a constructor where you can write fancy logic or the same design, and it will give out millions of options, the stricter the logic, the fewer options, but more corresponding code, i.e. everything will work, and the current difficulties, invented by ill-conceived developers, will be forgotten like a bad dream,)))

Maybe you forget, that all this is only possible because some “ill-conceived” developers had developed it sometimes ago.

Everything complicated or incomprehensible someday ends, it’s one thing to decide what people would understand and this will increase the service life, and it’s another thing to think that everything is fine anyway, so such tools die quickly, I’m sure the start of JS, it was in demand but a bunch of other tools came out, how could they come out so quickly if JS was a well thought out language to understand? I don’t think so, it can’t be otherwise.