Javacript Random Number Add Front Number

I am using the following JavaScript to create a random confirmation number.

<script>
  function randomNumber(len) {
    var randomNumber;
    var n = '';

    for(var count = 0; count < len; count++) {
        randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
    }
    return n;
}

document.getElementById("confirm").value = randomNumber(6);

</script>

I would like to add a letter in front of my number confirmation. Example " B-[random number here]". However, when I try I kill my JavaScript.

Any help would be appropriated.

Hi there mickeyatty1,

does this help…

<input id="confirm" type="text">

<script>

  function randomNumber(len) {
    var randomNumber;
    var n = '';
    var letters = [ 'A','B','C','D','E','F','G','H','I','J','K','M' ];
    var l = letters[ Math.floor(Math.random() * letters.length )];
    for( var count = 0; count < len; count ++ ) {
        randomNumber = Math.floor( Math.random() * 10 );
        n += randomNumber.toString();
    }
    n = l  + '-' +  n;
    return n;
 }

   document.getElementById( 'confirm' ).value = randomNumber(6);

</script>

coothead

1 Like

Thank you so much!

 
 
          No problem, you’re very welcome. :winky:
 
 
      coothead

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.