User:Elominius/random character generator.js

From Wikiversity
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* random character generator */
/* Mobile users can test this script by copying it and pasting it after "javascript:" in the web browser's URL address bar. */

/* array which lists the characters */
var characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var select_char = 0; /* Initializing variable to defeat JSHint warning; no functional difference. */

function random_char() {
/* randomly selects a position inside the array */
select_char = Math.floor(Math.random() * characters.length);

/* Prevents an error in the unlikely event that Math.random() results into exactly 1. It is unsure if it ever happens, but for being on the safe side. */
if (select_char == characters.length) select_char-=1;

/* returns character from selected position */
return characters[select_char];
}

/* alert character for demonstration purposes */
alert(random_char());