1

Phone number must be 9 digits (something like this 597257593)also I have to type first 3 digits (597 582167) and after that program has to generate last six number randomly.

I'm using Html and Javascipt.

<body>
    <h3 id="numbOutp">5</h3>
       
    <button onClick="pushFullNumb()">
        generateNumber
    </button>

    <script src="./main.js"></script>
</body>

Javascript:

var numArr = [""]

function makeRandomNumb() {
    var random = Math.floor(Math.random() * 10)
    let stringedNumb = random.toString()
    numArr.join(" ")
    return stringedNumb    
}

function pushFullNumb() {    
  for (i=0; i<6; i++) {
    numArr.push(makeRandomNumb())
  }    
  return numbOutp.innerText = `597` + " "  + numArr.join("") 
}

after this code runs it can make first full number but I want to make all the possible numbers and give it to element.

Thank you for any advice.

var numArr = [""]

function makeRandomNumb() {
    var random = Math.floor(Math.random() * 10)
    let stringedNumb = random.toString()
    numArr.join(" ")
    return stringedNumb    
}

function pushFullNumb() {    
  for (i=0; i<6; i++) {
    numArr.push(makeRandomNumb())
  }    
  return numbOutp.innerText = `597` + " "  + numArr.join("") 
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>random</title>
</head>
<body>
    <h3 id="numbOutp">5</h3>
       
    <button onClick="pushFullNumb()">
        generateNumber
    </button>

    <script src="./main.js"></script>
</body>

</html>
uski9992
  • 21
  • 1
  • 5
  • You want to generate each possible number? `597 ******`, are a lot of possibilities.. – 0stone0 Apr 03 '21 at 18:12
  • Yeah I want all of them, or at least some amount of them – uski9992 Apr 03 '21 at 18:14
  • there are 9^6 combinations – Andrew Nic Apr 03 '21 at 18:15
  • If you want each possibility, you can just loop from `0` to `999999`, for the first `100000`, pad a `0` on the left side. However, don't think any browser is gonna like this kind of actions. – 0stone0 Apr 03 '21 at 18:17
  • Yeah, I don't want this for browser, I used html to visualise it... Thank u, but what u mean, I have to loop from 0 to 999999, for the first 100000, pad a 0 on the left side. Can u give me code? – uski9992 Apr 03 '21 at 18:21

1 Answers1

2

I believe this is what you are looking for:

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

function getSomeNumbers(numsToGenerate) {
    first3Digits = "597"
    var allPhoneNumbers = [];
    for (i = 0; i < numsToGenerate; i++) {
        allPhoneNumbers.push(first3Digits + getRandomInt(100000, 999999))
    }
    return allPhoneNumbers;
}

console.log(getSomeNumbers(10))

This prints in the console as:

[
  '597811491', '597377764',
  '597368701', '597587388',
  '597261293', '597408272',
  '597483819', '597210418',
  '597208802', '597493061'
]

Attaching them to HTML might be something like the below code. This would be if you wanted to add 10 numbers to a div for each button click.

function OnClick(){
    numbersNeeded = 10;
    var numbers = getSomeNumbers(numbersNeeded);
    for (i = 0; i < numbersNeeded; i++) {
        document.getElementById("divId").innerHTML += "<li>"+numbers[i]+"</li>";
    }
}
Ntjs95
  • 128
  • 1
  • 8
  • 1
    Yeah, that's quite the answer but, how can i add all of them in h3 element or even some element like
    or
– uski9992 Apr 03 '21 at 18:37
  • @uski9992 at which point do you know how many you need to generate? – Ntjs95 Apr 03 '21 at 18:52
  • 1
    I've already added some code to determine how much number I need to generate – uski9992 Apr 03 '21 at 19:05
  • 1
    @uski9992 I added a code snippet of how to interact with html and these numbers. I hope it helps :) – Ntjs95 Apr 03 '21 at 19:13