<!DOCTYPE html><html><head><meta charset="utf-8"><title>Guid Number</title></head><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><body><button type="button" id="btnclick">Click Me!</button><script type="text/javascript">function uuidv4() {return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));}console.log(uuidv4());$('#btnclick').click(function(){alert(uuidv4());});</script></body></html>
Interviews1 answer
1 Answer
function generateGUID() {
return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == ‘x’ ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
```
This function generates a string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where x and y are hexadecimal digits generated randomly. The function replaces each x or y with a randomly generated digit using the Math.random() method and the toString(16) method to convert the result to hexadecimal.
Manoj Kumar DuraisamyPosted Feb 6, 2023, 8:51 PM EST

