skip to Main Content

I am currently making a word-guessing minigame. In this, I have one Array called PlayingArrays and a bunch of other arrays that contain names. I am using Math.random() to get the indices.

Question: How can I get the name of the array?

Note: The alert on line 19 in inside of playGame() is alert(`${category}`). When this runs, I get every word inside of the array.

const difficultyArticle = document.querySelector('#difficulty-article');
const difficultyOptions = document.querySelector('#difficulty-options');
const startGameButton = document.querySelector('#start-game');
const playArticle = document.querySelector('#play-article');
const resultArticle = document.querySelector('#result-article');

let maxTime = 0;
let currentTime = 0;
let currentSeconds = 0;
let currentMinutes = 0;
let lives = 0;
let difficulty;

function playGame() {
    let wordInfo = determineWord();
    let randomWord = wordInfo.word;
    let category = wordInfo.category;

    alert(`${randomWord}, ${category}`);
}

function determineWord() {
    let categoryIndex = Math.round(Math.random() * (PlayingArrays.length - 1));
    let category = PlayingArrays[categoryIndex];
    let wordIndex = Math.round(Math.random() * (category.length - 1));
    let randomWord = category[wordIndex];

    return {
        word: randomWord,
        category: category
    }
}

playArticle.classList.add("hidden");
resultArticle.classList.add("hidden");

startGameButton.addEventListener('click', (event) => {
    playGame();
})
<!DOCTYPE html>
<html lang="en">

<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE-edge">
        <meta name="viewport", content="width=device-width, initial-scale=1.0">
        <meta name="author" content="Christian Davis">
        <link rel="stylesheet" href="styles.css">
        <title>Guess That Word</title>
    </head>

    <body>
        <article id="difficulty-article">

            <div id="title-display">
                <h1>Guess That</h1>
                &nbsp;
                <h2>WORD!</h2>
            </div>

            <div id="difficulty-display">
                <select required id="difficulty-options">
                    <option value="">---Select Your Difficulty---</option>
                    <option value="Easy">Easy</option>
                    <option value="Medium">Medium</option>
                    <option value="Hard">Hard</option>
                    <option value="Impossible">Impossible</option>
                </select> <br>

                <button id="start-game">Start Game</button>
            </div>

            <img src="./Images/Question Mark Man.jpeg">
        </article>

        <article id="play-article">
            
        </article>

        <article id="result-article">
            
        </article>

        <script src="game.js"></script>
        <script src="Arrays.js"></script> 
        <!--Normally, we want to use type="module" in the game.js file,
        but that doesn't work on my chromebook-->
    </body>
</html>

Arrays:

const Animals = [
    "Aardvark",
    "Albatross",
    "Alligator",
    "Ant",
    "Armadillo",
    "Axolotl",
    "Aye-Aye",
    "Badger",
    "Banded Palm Civet",
    "Bandicoot",
    "Barnacle",
    "Barracuda",
    "Basilisk Lizard",
    "Basking Shark",
    "Bear",
    "Beaver",
    "Bedbug",
    "Bee",
    "Beetle",
    "Binturong",
    "Bird",
    "Birds Of Paradise",
    "Bison",
    "Blobfish",
    "Booby",
    "Bongo",
    "Bonito Fish",
    "Bonobo",
    "Booby",
    "Bovid",
    "Buffalo",
    "Bug",
    "Butterfly",
    "Buzzard",
    "Carp",
    "Cat",
    "Cheetah",
    "Chameleon",
    "Cockroach",
    "Cod",
    "Coelacanth",
    "Cheetah",
    "Chameleon",
    "Dolphin",
    "Duck",
    "Elephant",
    "Eagle",
    "Fox",
    "Frog",
    "Giraffe",
    "Gorilla",
    "Hawk",
    "Hippopotamus",
    "Iguana",
    "Ibis",
    "Jaguar",
    "Jellyfish",
    "Kangaroo",
    "Koala",
    "Lemur",
    "Lion",
    "Meerkat",
    "Monkey",
    "Narwhal",
    "Newt",
    "Orangutan",
    "Ostrich",
    "Penguin",
    "Peacock",
    "Quetzal",
    "Quokka",
    "Raccoon",
    "Rhinoceros",
    "Shark",
    "Sloth",
    "Tiger",
    "Toucan",
    "Uakari",
    "Umbrellabird",
    "Viper",
    "Vulture",
    "Wallaby",
    "Wombat",
    "X-ray Tetra",
    "Xenops",
    "Yabby",
    "Yak",
    "Zebra",
    "Zorse",
    "Albatross",
    "Aye-Aye",
    "Badger",
    "Banded Palm Civet",
    "Bandicoot",
    "Barnacle",
    "Barracuda",
    "Basilisk Lizard",
    "Basking Shark",
    "Bison",
    "Blobfish",
    "Booby",
    "Bongo",
    "Bonito Fish",
    "Bonobo",
    "Bovid",
    "Buffalo",
    "Bug",
    "Butterfly",
    "Buzzard",
    "Carp",
    "Cat",
    "Cockroach",
    "Cod",
    "Coelacanth",
    "Cuttlefish",
    "Dung Beetle",
    "Earwig",
    "Echidna",
    "Falcon",
    "Fiddler Crab",
    "Firefly",
    "Flatworm",
    "Flying Squirrel",
    "Gazelle",
    "Gecko",
    "Gibbon",
    "Glass Lizard",
    "Glow Worm",
    "Gnu",
    "Goblin Shark",
    "Goldfish",
    "Goose",
    "Gopher",
    "Gudgeon",
    "Guinea Pig",
    "Guppy",
    "Hamster",
    "Harpy Eagle",
    "Hercules Beetle",
    "Hermit Crab",
    "Herring",
    "Hornet",
    "Hummingbird",
    "Hyena",
    "Ibex",
    "Impala",
    "Indigo Bunting",
    "Insect",
    "Jackal",
    "Jerboa",
    "Junglefowl",
    "Kinkajou",
    "Kiwi",
    "Kookaburra",
    "Kowari",
    "Lamprey",
    "Leaf-Tailed Gecko",
    "Leech",
    "Lemming",
    "Limpet",
    "Lobster",
    "Locust",
    "Mantis Shrimp",
    "Mantis",
    "Marmoset",
    "Marsupial",
    "Mastodon",
    "Mayfly",
    "Midge",
    "Millipede",
    "Mole",
    "Mollusk",
    "Mongoose",
    "Monitor Lizard",
    "Moth",
    "Mouse",
    "Mule",
    "Nightingale",
    "Nudibranch",
    "Numbat",
    "Nurse Shark",
    "Okapi",
    "Olm",
    "Opossum",
    "Owl",
    "Panda",
    "Parrot",
    "Peafowl",
    "Pelican",
    "Penguin",
    "Pheasant",
    "Pig",
    "Pika",
    "Piranha",
    "Planarian",
    "Platypus",
    "Poodle",
    "Porcupine",
    "Quail",
    "Quokka",
    "Quoll",
    "Rabbit",
    "Rat",
    "Reindeer",
    "Reticulated Python",
    "Rhinoceros",
    "Sable",
    "Salamander",
    "Sardine",
    "Scorpion",
    "Sea Dragon",
    "Sea Slug",
    "Sea Squirt",
    "Sea Urchin",
    "Seahorse",
    "Seal",
    "Shrimp",
    "Skunk",
    "Sloth",
    "Slug",
    "Snail",
    "Snake",
    "Snipe",
    "Snow Leopard",
    "Sparrow",
    "Spider",
    "Sponge",
    "Squid",
    "Squirrel",
    "Star-Nosed Mole",
    "Starfish",
    "Stonefish",
    "Sturgeon",
    "Sun Bear",
    "Swordfish",
    "Tapeworm",
    "Tarantula",
    "Tarsier",
    "Termite",
    "Thorny Devil",
    "Tiger Beetle",
    "Tiger Shark",
    "Tiger Snake",
    "Tiglon",
    "Tortoise",
    "Toucan",
    "Tree Frog",
    "Tsetse Fly",
    "Uakari",
    "Uguisu",
    "Umbrellabird",
    "Unicorn",
    "Urial",
    "Vampire Bat",
    "Vampire Squid",
    "Vicuna",
    "Vulture",
    "Wallaby",
    "Walrus",
    "Warthog",
    "Wasp",
    "Water Buffalo",
    "Weasel",
    "Weevil",
    "Whale",
    "Whippet",
    "Whitefish",
    "Wild Boar",
    "Wildebeest",
    "Wolf",
    "Wolverine",
    "Wombat",
    "Woodpecker",
    "Wrasse",
    "Wrench",
    "Wryneck",
    "X-Ray Tetra",
    "X-Ray Fish",
    "Xantus's Hummingbird",
    "Xeme",
    "Xenarthra",
    "Xenomorph",
    "Xenophyophore",
    "Xenops",
    "Xenopus",
    "Xerus",
    "Xiaosaurus",
    "Xoloitzcuintli",
    "X-Ray Crab",
    "X-Ray Shrimp",
    "Yaffle",
    "Yak",
    "Yapok",
    "Yardang",
    "Yellow-Eyed Penguin",
    "Yellowjacket",
    "Yelling Cat",
    "Yerevan",
    "Yeti Crab",
    "Yew",
    "Ylva",
    "Yo-Yo",
    "Yodeler",
    "Yoshi",
    "Yowie",
    "Yoyo",
    "Yurt",
    "Zander",
    "Zebra",
    "Zebu",
    "Zedonk",
    "Zebra Shark",
    "Zebra Turkeyfish",
    "Zorse"
]

const People = [
    { firstName: "Christian", lastName: "Davis" },
    { firstName: "Ada", lastName: "Lovelace" },
    { firstName: "Abraham", lastName: "Lincoln" },
    { firstName: "Albert", lastName: "Einstein" },
    { firstName: "Alice", lastName: "Johnson" },
    { firstName: "Barack", lastName: "Obama" },
    { firstName: "Bob", lastName: "Smith" },
    { firstName: "Cleopatra", lastName: "Jones" },
    { firstName: "Charles", lastName: "Darwin" },
    { firstName: "Charlie", lastName: "Davis" },
    { firstName: "Coco", lastName: "Chanel" },
    { firstName: "David", lastName: "Bowie" },
    { firstName: "Emily", lastName: "Brown" },
    { firstName: "Emmeline", lastName: "Pankhurst" },
    { firstName: "Frank", lastName: "Johnson" },
    { firstName: "Frida", lastName: "Kahlo" },
    { firstName: "Galileo", lastName: "Galilei" },
    { firstName: "Grace", lastName: "Taylor" },
    { firstName: "Grace", lastName: "Hopper" },
    { firstName: "Henry", lastName: "Clark" },
    { firstName: "Helen", lastName: "Keller" },
    { firstName: "Ivy", lastName: "Robinson" },
    { firstName: "Isaac", lastName: "Newton" },
    { firstName: "Jack", lastName: "Anderson" },
    { firstName: "Jane", lastName: "Austen" },
    { firstName: "John", lastName: "Doe" },
    { firstName: "John", lastName: "Kennedy" },
    { firstName: "Katherine", lastName: "Miller" },
    { firstName: "Kylie", lastName: "Johnson" },
    { firstName: "Leonardo", lastName: "da Vinci" },
    { firstName: "Louis", lastName: "Turner" },
    { firstName: "Mia", lastName: "Hill" },
    { firstName: "Marie", lastName: "Curie" },
    { firstName: "Martin", lastName: "King Jr." },
    { firstName: "Nelson", lastName: "Mandela" },
    { firstName: "Noah", lastName: "Parker" },
    { firstName: "Olivia", lastName: "Rossi" },
    { firstName: "Pablo", lastName: "Picasso" },
    { firstName: "Paul", lastName: "White" },
    { firstName: "Quinn", lastName: "Miller" },
    { firstName: "Queen", lastName: "Elizabeth II" },
    { firstName: "Robert", lastName: "Davis" },
    { firstName: "Rosa", lastName: "Parks" },
    { firstName: "Sophia", lastName: "Walker" },
    { firstName: "Steve", lastName: "Jobs" },
    { firstName: "Thomas", lastName: "Edison" },
    { firstName: "Tom", lastName: "Thompson" },
    { firstName: "Ursula", lastName: "Smith" },
    { firstName: "Victor", lastName: "Garcia" },
    { firstName: "Vincent", lastName: "van Gogh" },
    { firstName: "Wendy", lastName: "Jones" },
    { firstName: "William", lastName: "Shakespeare" },
    { firstName: "Winston", lastName: "Churchill" },
    { firstName: "Xander", lastName: "Johnson" },
    { firstName: "Yvonne", lastName: "Miller" },
    { firstName: "Zachary", lastName: "Wilson" },
    { firstName: "Zhang", lastName: "Heng" }
]

const Buildings = [
    "Angkor Wat",
    "Big Ben",
    "Brandenburg Gate",
    "Burj Khalifa",
    "Christ the Redeemer",
    "Colosseum",
    "Eiffel Tower",
    "Forbidden City",
    "Great Wall of China",
    "Hagia Sophia",
    "Louvre Museum",
    "Machu Picchu",
    "Neuschwanstein Castle",
    "Petra",
    "Pyramids of Giza",
    "Sagrada Familia",
    "Sydney Opera House",
    "Taj Mahal",
    "The White House",
    "Tokyo Tower",
    "Alhambra",
    "Amsterdam Centraal",
    "Atomium",
    "Basilica of the Sagrada Familia",
    "Blue Mosque",
    "Buckingham Palace",
    "Casa Milà",
    "Casa Batlló",
    "Château de Versailles",
    "Cologne Cathedral",
    "Dresden Frauenkirche",
    "Dome of the Rock",
    "Edinburgh Castle",
    "Empire State Building",
    "Fallingwater",
    "Florence Cathedral",
    "Gherkin",
    "Golden Gate Bridge",
    "Great Mosque of Djenne",
    "Himeji Castle",
    "Hofburg",
    "Hollywood Sign",
    "Jin Mao Tower",
    "Leaning Tower of Pisa",
    "Mont Saint-Michel",
    "Moscow Kremlin",
    "Palace of Westminster",
    "Pantheon",
    "Potala Palace",
    "Reichstag Building",
    "Rockefeller Center",
    "St. Basil's Cathedral",
    "St. Peter's Basilica",
    "Statue of Liberty",
    "Stonehenge",
    "Taipei 101",
    "The Shard",
    "Tower Bridge",
    "Uffizi Gallery",
    "Willis Tower",
    "Windsor Castle",
    "Yellow Crane Tower"
]

const Climates = [
    "Alpine",
    "Arid",
    "Boreal Forest",
    "Chaparral",
    "Coastal",
    "Cold Alpine",
    "Cold Arid",
    "Cold Continental",
    "Cold Desert",
    "Cold Highland",
    "Cold Monsoon",
    "Cold Oceanic",
    "Cold Prairie",
    "Cold Rainforest",
    "Cold Savannah",
    "Cold Steppe",
    "Cold Swamp",
    "Cold Temperate",
    "Cold Tundra",
    "Cold Wetland",
    "Cold Oceanic",
    "Cold Ice Cap",
    "Desert",
    "Dry Continental",
    "Dry Desert",
    "Dry Highland",
    "Dry Ice Cap",
    "Dry Oceanic",
    "Dry Prairie",
    "Dry Rainforest",
    "Dry Savannah",
    "Dry Steppe",
    "Dry Swamp",
    "Dry Tundra",
    "Dry Wetland",
    "Equatorial",
    "Equatorial Guinea Climate",
    "Equatorial Monsoon",
    "Equatorial Rainforest",
    "Hot Alpine",
    "Hot Arid",
    "Hot Continental",
    "Hot Desert",
    "Hot Highland",
    "Hot Ice Cap",
    "Hot Oceanic",
    "Hot Prairie",
    "Hot Rainforest",
    "Hot Savannah",
    "Hot Steppe",
    "Hot Swamp",
    "Hot Tundra",
    "Hot Wetland",
    "Hot Oceanic",
    "Hot Steppe",
    "Hot Savannah",
    "Hot Prairie",
    "Hot Swamp",
    "Hot Alpine",
    "Hot Rainforest",
    "Hot Monsoon",
    "Hot Desert",
    "Hot Highland",
    "Hot Tundra",
    "Hot Ice Cap",
    "Humid Continental",
    "Humid Subtropical",
    "Ice Cap",
    "Mangrove",
    "Mediterranean",
    "Mediterranean Forest",
    "Monsoon",
    "Mountain",
    "Polar",
    "Polar Alpine",
    "Polar Desert",
    "Polar Ice Cap",
    "Polar Maritime",
    "Polar Tundra",
    "Polar Wetland",
    "Polar Oceanic",
    "Prairie",
    "Savanna",
    "Semi-Arid",
    "Steppe",
    "Subarctic",
    "Subtropical Desert",
    "Subtropical Highland",
    "Subtropical Oceanic",
    "Subtropical Steppe",
    "Subtropical Wetland",
    "Swamp",
    "Taiga",
    "Temperate Oceanic",
    "Temperate Rainforest",
    "Thundra",
    "Tropical Arid",
    "Tropical Continental",
    "Tropical Desert",
    "Tropical Highland",
    "Tropical Monsoon",
    "Tropical Oceanic",
    "Tropical Rainforest",
    "Tropical Savannah",
    "Tropical Wetland",
    "Tundra",
    "Warm Arid",
    "Warm Oceanic",
    "Warm Steppe",
    "Wetland",
    "Xanthus's Hummingbird",
    "Xeme",
    "Xenomorph",
    "Xenophyophore",
    "Xenops",
    "Xenopus",
    "Xerus",
    "Xiaosaurus",
    "Xoloitzcuintli",
    "X-Ray Crab",
    "X-Ray Fish",
    "X-Ray Shrimp",
    "Yellow-Eyed Penguin",
    "Yellowjacket",
    "Yelling Cat",
    "Yerevan",
    "Yeti Crab",
    "Yew",
    "Ylva",
    "Yo-Yo",
    "Yodeler",
    "Yoshi",
    "Yowie",
    "Yoyo",
    "Yurt",
    "Zander",
    "Zebra",
    "Zebra Shark",
    "Zebra Turkeyfish",
    "Zedonk"
]

const Countries = [
    "Afghanistan",
    "Albania",
    "Algeria",
    "American Samoa",
    "Andorra",
    "Angola",
    "Anguilla",
    "Antarctica",
    "Antigua and Barbuda",
    "Argentina",
    "Armenia",
    "Aruba",
    "Australia",
    "Austria",
    "Azerbaijan",
    "Bahamas",
    "Bahrain",
    "Bangladesh",
    "Barbados",
    "Belarus",
    "Belgium",
    "Belize",
    "Benin",
    "Bermuda",
    "Bhutan",
    "Bolivia",
    "Bonaire", 
    "Sint Eustatius",
    "Saba",
    "Bosnia and Herzegovina",
    "Botswana",
    "Bouvet Island",
    "Brazil",
    "British Indian Ocean Territory",
    "Brunei Darussalam",
    "Bulgaria",
    "Burkina Faso",
    "Burundi",
    "Cabo Verde",
    "Cambodia",
    "Cameroon",
    "Canada",
    "Cayman Islands",
    "Central African Republic",
    "Chad",
    "Chile",
    "China",
    "Christmas Island",
    "Cocos Islands",
    "Colombia",
    "Comoros",
    "Democratic Republic of the Congo",
    "The Congo",
    "Cook Islands",
    "Costa Rica",
    "Croatia",
    "Cuba",
    "Curaçao",
    "Cyprus",
    "Czechia",
    "Côte d'Ivoire",
    "Denmark",
    "Djibouti",
    "Dominica",
    "Dominican Republic",
    "Ecuador",
    "Egypt",
    "El Salvador",
    "Equatorial Guinea",
    "Eritrea",
    "Estonia",
    "Eswatini",
    "Ethiopia",
    "Falkland Islands",
    "Faroe Islands",
    "Fiji",
    "Finland",
    "France",
    "French Guiana",
    "French Polynesia",
    "Gabon",
    "Gambia",
    "Georgia",
    "Germany",
    "Ghana",
    "Gibraltar",
    "Greece",
    "Greenland",
    "Grenada",
    "Guadeloupe",
    "Guam",
    "Guatemala",
    "Guernsey",
    "Guinea",
    "Guinea-Bissau",
    "Guyana",
    "Haiti",
    "Heard Island",
    "Holy See",
    "Honduras",
    "Hong Kong",
    "Hungary",
    "Iceland",
    "India",
    "Indonesia",
    "Islamic Republic of Iran",
    "Iraq",
    "Ireland",
    "Isle of Man",
    "Israel",
    "Italy",
    "Jamaica",
    "Japan",
    "Jersey",
    "Jordan",
    "Kazakhstan",
    "Kenya",
    "Kiribati",
    "Democratic People's Republic of Korea",
    "Republic of Korea",
    "Kuwait",
    "Kyrgyzstan",
    "Lao People's Democratic Republic",
    "Latvia",
    "Lebanon",
    "Lesotho",
    "Liberia",
    "Libya",
    "Liechtenstein",
    "Lithuania",
    "Luxembourg",
    "Macao",
    "Madagascar",
    "Malawi",
    "Malaysia",
    "Maldives",
    "Mali",
    "Malta",
    "Marshall Islands",
    "Martinique",
    "Mauritania",
    "Mauritius",
    "Mayotte",
    "Mexico",
    "Micronesia",
    "Moldova",
    "Monaco",
    "Mongolia",
    "Montenegro",
    "Montserrat",
    "Morocco",
    "Mozambique",
    "Myanmar",
    "Namibia",
    "Nauru",
    "Nepal",
    "Netherlands",
    "New Caledonia",
    "New Zealand",
    "Nicaragua",
    "Niger",
    "Nigeria",
    "Niue",
    "Norfolk Island",
    "Mariana Islands",
    "Norway",
    "Oman",
    "Pakistan",
    "Palau",
    "Palestine",
    "Panama",
    "Papua New Guinea",
    "Paraguay",
    "Peru",
    "Philippines",
    "Pitcairn",
    "Poland",
    "Portugal",
    "Puerto Rico",
    "Qatar",
    "Macedonia",
    "Romania",
    "Russia",
    "Rwanda",
    "Réunion",
    "Saint Barthélemy",
    "Saint Helena",
    "Saint Kitts",
    "Saint Lucia",
    "Saint Martin",
    "Saint Pierre",
    "Saint Vincent",
    "Samoa",
    "San Marino",
    "Sao Tome and Principe",
    "Saudi Arabia",
    "Senegal",
    "Serbia",
    "Seychelles",
    "Sierra Leone",
    "Singapore",
    "Sint Maarten",
    "Slovakia",
    "Slovenia",
    "Solomon Islands",
    "Somalia",
    "South Africa",
    "South Georgia",
    "South Sudan",
    "Spain",
    "Sri Lanka",
    "Sudan",
    "Suriname",
    "Svalbard and Jan Mayen",
    "Sweden",
    "Switzerland",
    "Syrian Arab Republic",
    "Taiwan",
    "Tajikistan",
    "Tanzania",
    "Thailand",
    "Timor-Leste",
    "Togo",
    "Tokelau",
    "Tonga",
    "Trinidad and Tobago",
    "Tunisia",
    "Turkey",
    "Turkmenistan",
    "Turks and Caicos",
    "Tuvalu",
    "Uganda",
    "Ukraine",
    "United Arab Emirates",
    "United Kingdom",
    "United States",
    "Uruguay",
    "Uzbekistan",
    "Vanuatu",
    "Venezuela",
    "Vietnam",
    "Virgin Islands",
    "Virgin Islands",
    "Wallis and Futuna",
    "Western Sahara",
    "Yemen",
    "Zambia",
    "Zimbabwe",
    "Åland Islands"
]

const States = [
    "Alabama",
    "Alaska",
    "Arizona",
    "Arkansas",
    "California",
    "Colorado",
    "Connecticut",
    "Delaware",
    "Florida",
    "Georgia",
    "Hawaii",
    "Idaho",
    "Illinois",
    "Indiana",
    "Iowa",
    "Kansas",
    "Kentucky",
    "Louisiana",
    "Maine",
    "Maryland",
    "Massachusetts",
    "Michigan",
    "Minnesota",
    "Mississippi",
    "Missouri",
    "Montana",
    "Nebraska",
    "Nevada",
    "New Hampshire",
    "New Jersey",
    "New Mexico",
    "New York",
    "North Carolina",
    "North Dakota",
    "Ohio",
    "Oklahoma",
    "Oregon",
    "Pennsylvania",
    "Rhode Island",
    "South Carolina",
    "South Dakota",
    "Tennessee",
    "Texas",
    "Utah",
    "Vermont",
    "Virginia",
    "Washington",
    "West Virginia",
    "Wisconsin",
    "Wyoming"
]

const Capitals = [
    "Montgomery",
    "Juneau",
    "Phoenix",
    "Little Rock",
    "Sacramento",
    "Denver",
    "Hartford",
    "Dover",
    "Tallahassee",
    "Atlanta",
    "Honolulu",
    "Boise",
    "Springfield",
    "Indianapolis",
    "Des Moines",
    "Topeka",
    "Frankfort",
    "Baton Rouge",
    "Augusta",
    "Annapolis",
    "Boston",
    "Lansing",
    "St. Paul",
    "Jackson",
    "Jefferson City",
    "Helena",
    "Lincoln",
    "Carson City",
    "Concord",
    "Trenton",
    "Santa Fe",
    "Albany",
    "Raleigh",
    "Bismarck",
    "Columbus",
    "Oklahoma City",
    "Salem",
    "Harrisburg",
    "Providence",
    "Columbia",
    "Pierre",
    "Nashville",
    "Austin",
    "Salt Lake City",
    "Montpelier",
    "Richmond",
    "Olympia",
    "Charleston",
    "Madison",
    "Cheyenne",
    "Tokyo",       // Japan
    "Paris",       // France
    "Berlin",      // Germany
    "Cairo",       // Egypt
    "Moscow",      // Russia
    "Seoul",       // South Korea
    "Oslo",        // Norway
    "Wellington",  // New Zealand
    "Beijing",     // China
    "Rome",        // Italy
    "Dublin",      // Ireland
    "Canberra",    // Australia
    "Nairobi",     // Kenya
    "Bangkok",     // Thailand
    "Stockholm",   // Sweden
    "Amsterdam",   // Netherlands
    "Brasília",    // Brazil
    "Lisbon",      // Portugal
    "Ottawa"       // Canada
]

const Foods = [
    "Almond",
    "Apple",
    "Apple Pie",
    "Asparagus",
    "Banana",
    "Barley",
    "Beef",
    "Broccoli",
    "Brownie",
    "Butter",
    "Candy",
    "Carrot",
    "Cheese",
    "Cherry",
    "Chicken",
    "Couscous",
    "Daikon",
    "Date",
    "Doughnut",
    "Duck",
    "Durian",
    "Eclair",
    "Egg",
    "Eggplant",
    "Elderberry",
    "Einkorn",
    "Evaporated Milk",
    "Fennel",
    "Feta",
    "Fig",
    "Fish",
    "Fudge",
    "Gelato",
    "Garlic",
    "Grapes",
    "Grits",
    "Grouper",
    "Habanero",
    "Halibut",
    "Havarti",
    "Hempseed",
    "Honeydew",
    "Hot Fudge Sundae",
    "Ice Cream",
    "Ice Cream Sandwich",
    "Iceberg Lettuce",
    "Israeli Couscous",
    "Jalapeño Jack",
    "Jelly Beans",
    "Jellyfish",
    "Kiwi",
    "Kale",
    "Kamut",
    "Kettle Corn",
    "Lamb",
    "Lemon",
    "Lemon Bars",
    "Lettuce",
    "Macarons",
    "Mango",
    "Milk",
    "Mousse",
    "Mushroom",
    "Mussels",
    "Napa Cabbage",
    "Natto",
    "Nondairy Yogurt",
    "Nougat",
    "Oatmeal Raisin Cookies",
    "Olive Oil",
    "Orange",
    "Ostrich",
    "Papaya",
    "Parmesan",
    "Peach",
    "Pear",
    "Pecan",
    "Pepper",
    "Pesto",
    "Pineapple",
    "Pork",
    "Potato",
    "Pudding",
    "Pumpkin",
    "Quark",
    "Quiche",
    "Quinoa",
    "Raspberry",
    "Rice",
    "Sausage",
    "Shrimp",
    "Sorghum",
    "Soy Milk",
    "Spinach",
    "Strawberry",
    "Sushi",
    "Taco",
    "Tapioca",
    "Tea",
    "Teff",
    "Toffee",
    "Udon",
    "Vanilla",
    "Walnut",
    "Watermelon",
    "Wonton",
    "Yogurt"
]

const PlayingArrays = [
    Animals, 
    People,
    Buildings,
    Climates,
    Countries,
    States,
    Capitals,
    Foods
]

2

Answers


  1. How can I get the name of the array?

    You can’t. When you add arrays like Animals, People, etc into PlayingArrays, all you’ve got is a 2-dimensional array. The variable names are not kept. I’d suggest using an object instead

    const PlayingArrays = {
      Animals,
      People,
      Buildings,
      Climates,
      Countries,
      States,
      Capitals,
      Foods,
    };
    

    Then you can use the object keys to get your category

    const categories = Object.keys(PlayingArrays);
    const category = categories[(Math.floor(Math.random() * categories.length)];
    const words = PlayingArrays[category];
    const randomWord = words[Math.floor(Math.random() * words.length)];
    
    Login or Signup to reply.
  2. Getting the name of the variable doesn’t make that much sense, I think a better way might be to have another array of the names and use the same indexes:

    const PlayingArrayNames = [
        'Animals', 
        'People',
        'Buildings',
        'Climates',
        'Countries',
        'States',
        'Capitals',
        'Foods'
    ]
    

    Then you can just use the same index to get the names.

    Or you could modify your PlayingArrays array to be an array of objects, with a name field. It does mean you would have to reference the array inside the object before you use it.

    const PlayingArray = [
        {name: 'Animals', values: Animals}, 
        {name: 'People', values: People},
        {name: 'Buildings', values: Buildings},
        {name: 'Climates', values: Climates},
        {name: 'Countries', values: Countries},
        {name: 'States', values: States},
        {name: 'Capitals', values: Capitals},
        {name: 'Foods', values: Foods}
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search