stevesimonson
stevesimonson

Reputation: 13

Generate v4 UUID from Specific Javascript

My boss has asked me to follow this guide for generating a UUID from this link: https://intercom.help/revcontent2/en/articles/3436818-html-ads-generate-your-own-uuids

But I have no clue how to do it. I tried adding it to a file called test.js and opening it on Windows. I tried installing node.js and opening it through command prompt. I pasted it into a fiddle. But no success.

I just need a UUID generated from this code:

function uuid() {    
    var uuid = "", i, random;    

    for (i = 0; i < 32; i++) {      
        random = Math.random() * 16 | 0;        

        if (i == 8 || i == 12 || i == 16 || i == 20) {        
            uuid += "-";      
        }

        uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);    
     }   

     return uuid;  
}

Can someone please help me? Or just generate a code tell me what it is?

Thank you :)

Upvotes: 1

Views: 7522

Answers (1)

Nicolae Olariu
Nicolae Olariu

Reputation: 2555

First of all, please have a look and search for other threads before creating a new question. Like this one.

This MDN article is also a very good start.

Secondly, for things like this (executing a specific JS code), you can:

  • open your browser
  • go to Tools -> Developer Menu (or use specific Windows/Unix shortcuts to get here)
  • go to Console
  • paste your code above
  • then call your method, in your case, call uuid()
  • and voila! You can see the results!

Sample uuids generated by your code:

  • 7fc76c58-54b3-4e03-a743-64905a56a0bb
  • 2199fce0-abf9-4120-91eb-8096bd43ccf2
  • 443b49dc-6e0b-471e-8438-7e4819988726

Upvotes: 3

Related Questions