<title>
Module 0 - Assignment 1 - Array Example - Sheldon Pasciak
</title>
<p>Random Class Member Picker</p>
<p id="randomMemberName"></p>
<input type="button" id="buttonPickOne" value="Pick One" />
JavaScript
//Module 0 - Assignment 1 - Array Example - Sheldon Pasciak
//array of strings (first names of class members from ALL classlist)
var classMembers = ["Michael",
"Christopher",
"Taylor",
"Jeffrey",
"Devin",
"Bernard",
"Alex",
"Carlos",
"Dana",
"Kyle",
"Elgin",
"Austin",
"Jacob",
"John",
"Jason",
"Louise",
"Toniann",
"Robert",
"Shawn",
"Sheldon",
"Gabriel",
"Neekit",
"Marcus",
"Philip",
"Elvis",
"Evan",
"Brendan",
"Rupal",
"Michael",
"Kenneth",
"Eric"];
//picks a random class member from classMembers array of Strings
$('#buttonPickOne').click(function() {
var randomIndex;
if (classMembers.length<=0) {
alert("There aren't any class members to choose from.");
} else {
//note: [0] is the first element. Array indexes start with 0.
randomIndex = Math.ceil(Math.random() * classMembers.length);
document.getElementById("randomMemberName").innerHTML = "Your random pick is #" + randomIndex + "<br/><br/>" + classMembers[randomIndex-1];
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.