0

I would need some help to adjust this code to suit my needs.

I need to build a javascript that will be stored on a SharePoint page in order to generate on demand a NEW random USERID.

The problem is that I have zero knowledge of javascript, but I am very willing to learn.

The ID is built like this : "IT" & "number from 30001 to 79999"

Example: IT30002

The IDs created must always be different, so those "used" have to be permanently stored in a file.

Every time a new ID is prompted, the script will check the history file and provide a new ID.

Is there a way to achieve what I need?

I have looked at these 2 codes examples:

  1. This example has the key functionality of storing the previous choices, so I am sure I will not use the same ID twice, the problem is that I need numbers, not names and also I need the script to store the numbers permanently

  2. The code below has the other functionality of the "button" to press in order to display the ID.

<html>
<body>

<p id="one"></p>
<button onclick="random()">Random</button>

<script>
 function random(){

document.getElementById("one").innerHTML = Math.floor(Math.random() * 10);
}
</script>

</body>
THELUKE
  • 3
  • 3

3 Answers3

0

Some references to concepts for you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set and https://www.w3schools.com/jsref/prop_win_localstorage.asp

Well, all you need to do is generate a random number and store it somewhere. Since, you're using javascript at the front-end, you can't write onto a file without using some backend. If you're fine with storing things for a single user's session, you can use localstorage to store your previous findings in a Set. This lets you store stuff in the browser but for a single user only.

Now the question is what to store? Everytime you generate a random number, look for it in the set from localstorage. If it exists, generate the random number again and repeat lookup process. Keep repeating this random number generation and lookup process until a new random number is found.

What to do if a new random number is finally generated i.e. it doesn't exist in the set? You store it into the set and save the set to the localstorage, stop repeating the process and use the newly generated number for your needs.

That's it.

VPaul
  • 1,005
  • 11
  • 21
  • Thank you @ Rishinder , I need to store the already generated numbers permanently for all users and, also, what backed can I use to do so? What is the easiest one? Simple TXT file? – THELUKE Nov 29 '19 at 14:17
0

@Rishinder has explained some of your possible approaches. I will share some code to explain the same. This will print the random numbers on the browser

<html>
<body>
    <div id="random"></div>
    <button onclick="printRandom()">Generate numbers</button>
</body>
<script>
    // fetch and prepare the data
    var storage = localStorage.getItem('random'),
        existingNumbers = storage ? JSON.parse(storage) : {};

    // will generate the random number
    function generateRandom() {
        var loop = true,
            newRand;
        while (loop) {
            newRand = Math.ceil(Math.random() * 1000000) % 79999;
            if (existingNumbers[newRand])
                continue;
            else if (!existingNumbers[newRand] && newRand < 30001)
                continue;
            else if (!existingNumbers[newRand]) {
                existingNumbers[newRand] = true;
                localStorage.setItem('random', JSON.stringify(existingNumbers));
                loop = false;
            }
        }
        return Object.keys(existingNumbers).join(', ');
    }
    // print the existing numbers already stored
    document.getElementById('random').innerHTML = Object.keys(existingNumbers).join(', ');

    function printRandom() {
        document.getElementById('random').innerHTML = generateRandom();
    }
</script>
</html>

Hope this helps

Sam
  • 46
  • 4
  • Thank you @Sam for the prompt help, I have realized that this is way above my skillset: as much as I would like to, I do not have enough time to learn the code to build the integration with a back-end Database where the already generated numbers would need to be stored permanently. Instead I am investigating a simple Access Web App or A SharePoint workflow. – THELUKE Dec 02 '19 at 08:48
0

randojs.com makes this pretty easy.

Put this in the head of your page:

<script src="https://randojs.com/1.0.0.js"></script>

And then you can use this JavaScript:

var sequence = randoSequence(30001, 79999);
var currentIndex = 0;
function getNewID(){
    return "IT" + sequence[currentIndex++];
}

And you can add this button to the body of your page if you need to:

<button onclick="alert(getNewID());">Alert new ID.</button>

Here's all of that together (click "Run" and then click the "Alert new ID." button that shows up to see it work):

var sequence = randoSequence(30001, 79999);
var currentIndex = 0;
function getNewID(){
  return "IT" + sequence[currentIndex++];
}
<script src="https://randojs.com/1.0.0.js"></script>
<button onclick="alert(getNewID());">Alert new ID.</button>
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15