Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,894 members, 7,802,877 topics. Date: Saturday, 20 April 2024 at 12:55 AM

Javascript Programers Help Me With This Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Javascript Programers Help Me With This Problem (1404 Views)

Abuja Programers Pls Help Me Na I Want To Learn Na Abeg / Why Success Is Far From Most Nigerian Programers / Programers In Abuja (2) (3) (4)

(1) (Reply) (Go Down)

Javascript Programers Help Me With This Problem by Venzee(m): 1:41am On Mar 02, 2020
I greet you all nairalanders.....

Please am working on a small complex project and I just got stuck in this problem since last week....

This is the problem am facing....

I want to generate a 12 base( 12 digits) random number that will be mailed to the user after registration, which will be his or her profile unique ID number..

Lemme explain it better...

For example, you signed up with us, the after registration will give u a unique ID number for login in to your account...

Now the problem is, the Math.random() function will sometimes generates exatexa the same number it has generated before
Now am looking for mathod that check if the the id number already exists, if I dose not exists generate another number but if it exists, ignore the ID number and generate another one .

That is the problem am facing because each user will have a unique ID number.

This is the codes I used to do it, but I doesn't work as expected..


// Please am using both ES5 and ES6

const user_id_generator = (
var IDs, id_dataDB, id_numb_limit;
IDs = [];

// base 12 id number
id_numb_limit = 999999999999;

//Generating random number
let generator =
Math.floor(Math.random(id_numb_limit)+1 );

// to add the random number to the IDs
IDs.push(generator);

function check_ID_number(){
//Old school for loop
for (var count = 0; count < IDs.length;
count++){
var create_new_id_number;
if (generator !== IDs[count]){
IDs.push(generator)

} else if (generator === IDs[count]{
create_new_id_number =
generator;
IDs.push(create_new_id_number)
}}
}
check_ID_number();
)();


This is just for the ID generator, but is not working as expected...

Please if you have any idea on how to solve this problem, please your comment is highly needed..

Or you can just inbox me via WhatsApp...
09037099739
Re: Javascript Programers Help Me With This Problem by AntiWailer: 4:48am On Mar 02, 2020
K
Re: Javascript Programers Help Me With This Problem by AntiWailer: 4:52am On Mar 02, 2020
U can generate the unique numbers from your database depending on the database u are using..

MsSql - identity field. (Once you insert the record, u can retrieve the unique number and padd it with something to form the id u need to use )

Oracle - generate a sequence and use the unique value to do whatever u need to do


I advice u also reconsider generating random Numbers as their login. The problem with your approach is that if u use an independent random generator from js like u try to do, when your user base grows, the likely hood of repeat will still be there no matter au awesome the random number generator is.

Let them choose something they can remember, validate it against your existing users to make sure it does not exist and allow them use it during registration.

Email, mobile numbers are also unique stuffs u can allow your users to use as their login details. You will store the information with unique constraints in your database so that even if client validation fail u, the database will not allow such duplicates.


I hope this helps.

2 Likes 1 Share

Re: Javascript Programers Help Me With This Problem by niel63(m): 4:54am On Mar 02, 2020
A quick glance through your code.... I noticed your random number GEN

//Generating random number
let generator =
Math.floor(Math.random(id_numb_limit)+1 );

Will always return 1.




TRY THIS

let generator = Math.floor (Math.random () * id_numb_limit + 1);

1 Like

Re: Javascript Programers Help Me With This Problem by Nobody: 4:56am On Mar 02, 2020
If you were using nodejs you could just use the random string dependency
Re: Javascript Programers Help Me With This Problem by Venzee(m): 7:16am On Mar 02, 2020
AntiWailer:
U can generate the unique numbers from your database depending on the database u are using..

MsSql - identity field. (Once you insert the record, u can retrieve the unique number and padd it with something to form the id u need to use )

Oracle - generate a sequence and use the unique value to do whatever u need to do


I advice u also reconsider generating random Numbers as their login. The problem with your approach is that if u use an independent random generator from js like u try to do, when your user base grows, the likely hood of repeat will still be there no matter au awesome the random number generator is.

Let them choose something they can remember, validate it against your existing users to make sure it does not exist and allow them use it during registration.

Email, mobile numbers are also unique stuffs u can allow your users to use as their login details. You will store the information with unique constraints in your database so that even if client validation fail u, the database will not allow such duplicates.


I hope this helps.


Thank you very much bro,
Lemme try that .. I acpreappre.
Re: Javascript Programers Help Me With This Problem by Venzee(m): 7:21am On Mar 02, 2020
niel63:
A quick glance through your code.... I noticed your random number GEN

//Generating random number
let generator =
Math.floor(Math.random(id_numb_limit)+1 );

Will always return 1.




TRY THIS

let generator = Math.floor (Math.random () * id_numb_limit + 1);



My brother that was typical error abeg...
Na just wen ah do fast to post am ...
No reason am I just went through it now
But thanks anyway for your correction...
Re: Javascript Programers Help Me With This Problem by peterincredible: 9:10am On Mar 02, 2020
if you are using node.js use the module called "uuid" to generate unique id and save it to the database when the user submit registration i hope that helps

1 Like

Re: Javascript Programers Help Me With This Problem by Deicide: 11:09am On Mar 02, 2020
Why are you using JavaScript for something like this? Stuff like this should be done on the server side! unless you're using nodejs. JavaScript can be easily tampered with.
Re: Javascript Programers Help Me With This Problem by Karleb(m): 12:14pm On Mar 02, 2020
Deicide:
Why are you using JavaScript for something like this? Stuff like this should be done on the server side! unless you're using nodejs. JavaScript can be easily tampered with.

I concur.

Just enter your user's data directly into the database and auto generate each user's id.
Re: Javascript Programers Help Me With This Problem by Venzee(m): 1:35pm On Mar 02, 2020
[quote Why are you using JavaScript for something like this? Stuff like this should be done on the server side! [/quote]
bros i understand, no be only wey ah dey use, i de use ajax,json & webpack modules d truth is dat ah won use 1 stone to kill 2 birds
Re: Javascript Programers Help Me With This Problem by Venzee(m): 1:50pm On Mar 02, 2020
Karleb:


I concur.

Just enter your user's data directly into the database and generate each user's id.
bros SQL no fee generate unique id,
d thing be say, ah just de avoide PHP, thats y ah de use main JS nd node.js
Re: Javascript Programers Help Me With This Problem by Karleb(m): 2:44pm On Mar 02, 2020
Venzee:

bros SQL no fee generate unique id,
d thing be say, ah just de avoide PHP, thats y ah de use main JS nd node.js

Actually SQL has an auto increment keyword that can automatically generate a unique id for any record entered.

You don't need php to use sql, you could use sql with node.js.

1 Like

Re: Javascript Programers Help Me With This Problem by ensodev(m): 4:01pm On Mar 02, 2020
The issue is not your code, cos your code will not help you here...your approach is the issue.

If you need a unique number for this, use MySQL as suggested but let me add this.

I believe each user will have an id, That is enough a unique number, if you can think deep you can always make it any length of digit you want.

Also if you can also create a column say uid and by the time you want to first register a user give the uid value of the least digit you want. Eg

Say you need 10digit
1000000000

Then the next user registering get
1000000001

The problem with this approach is that your users can assume others member ID.

Another way to do it is to use your backend language apart from JS to get the unique I'd but be sure that if you will always check the value for uniqueness each time a user signed it then you will experience slow site load as your user get bigger.

My best advice is you stick to the few unique data at hand..like email, id, timestamp of registration.

Have learnt never to make things complex for myself when it comes to programming.

1 Like

Re: Javascript Programers Help Me With This Problem by Nobody: 6:34pm On Mar 02, 2020
Install uuid from npm and save urself the stress
Re: Javascript Programers Help Me With This Problem by codeigniter(m): 10:50pm On Mar 07, 2020
Use uuid for generating unique IDs if u have problem use google and YouTube, most people here are PHP guys
Re: Javascript Programers Help Me With This Problem by Taofeekdboy(m): 11:01pm On Mar 07, 2020
Talking from django aspect, this will be done in the database, or follow uuid as aspect as that will save you time. This can't really be done on the client side rather the server side.
Re: Javascript Programers Help Me With This Problem by Yupng: 7:27am On Mar 08, 2020
WHY YOUR CODE DON'T SEEM TO WORK:

First, your loop will not even run, as its condition will always return false because your array length is 0. for(count = 0; ARRAY LENGTH = 0)

Secondly, for you to be able to store whatever ids you generate and compare to subsequent once, you will need to first store them in a Persistent storage or state management.

If you want to do this in the client side, you can use your localstorage or some state management - e.g Redux






Venzee:
I greet you all nairalanders.....

Please am working on a small complex project and I just got stuck in this problem since last week....

This is the problem am facing....

I want to generate a 12 base( 12 digits) random number that will be mailed to the user after registration, which will be his or her profile unique ID number..

Lemme explain it better...

For example, you signed up with us, the after registration will give u a unique ID number for login in to your account...

Now the problem is, the Math.random() function will sometimes generates exatexa the same number it has generated before
Now am looking for mathod that check if the the id number already exists, if I dose not exists generate another number but if it exists, ignore the ID number and generate another one .

That is the problem am facing because each user will have a unique ID number.

This is the codes I used to do it, but I doesn't work as expected..


// Please am using both ES5 and ES6

const user_id_generator = (
var IDs, id_dataDB, id_numb_limit;
IDs = [];

// base 12 id number
id_numb_limit = 999999999999;

//Generating random number
let generator =
Math.floor(Math.random(id_numb_limit)+1 );

// to add the random number to the IDs
IDs.push(generator);

function check_ID_number(){
//Old school for loop
for (var count = 0; count < IDs.length;
count++){
var create_new_id_number;
if (generator !== IDs[count]){
IDs.push(generator)

} else if (generator === IDs[count]{
create_new_id_number =
generator;
IDs.push(create_new_id_number)
}}
}
check_ID_number();
)();


This is just for the ID generator, but is not working as expected...

Please if you have any idea on how to solve this problem, please your comment is highly needed..

Or you can just inbox me via WhatsApp...
09037099739
Re: Javascript Programers Help Me With This Problem by Venzee(m): 11:47pm On Mar 09, 2020
Taofeekdboy:
Talking from django aspect, this will be done in the database, or follow uuid as aspect as that will save you time. This can't really be done on the client side rather the server side.

Abi..! Bros...
You don abeg
Re: Javascript Programers Help Me With This Problem by Venzee(m): 11:54pm On Mar 09, 2020
Yupng:
WHY YOUR CODE DON'T SEEM TO WORK:

First, your loop will not even run, as its condition will always return false because your array length is 0. for(count = 0; ARRAY LENGTH = 0)

Secondly, for you to be able to store whatever ids you generate and compare to subsequent once, you will need to first store them in a Persistent storage or state management.

If you want to do this in the client side, you can use your localstorage or some state management - e.g Redux







Bros thank you abeg
Make I go try the localStorage and store the IDs there..
You don try abeg..

What if I use cookies to store the IDs directly to the user's machine?..
Re: Javascript Programers Help Me With This Problem by crunchyDope(m): 9:57pm On Mar 11, 2020
Venzee:


Bros thank you abeg
Make I go try the localStorage and store the IDs there..
You don try abeg..

What if I use cookies to store the IDs directly to the user's machine?..
Lol!
Re: Javascript Programers Help Me With This Problem by Yupng: 10:55am On Mar 16, 2020
If there is a server side for you app, sure use cookies. I suggest LS because I thought your app was solely client side based.


Venzee:


Bros thank you abeg
Make I go try the localStorage and store the IDs there..
You don try abeg..

What if I use cookies to store the IDs directly to the user's machine?..

(1) (Reply)

. / I Need A Webmaster Who Can Create A Professional Website With Cpanel Emails. / Experienced PHP Website Developer Wanted In Ikorodu Lagos

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 41
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.