简体   繁体   中英

Card flip Javascript from more than one set

I am making a card-flip game in javascript, and would like to be able to pull out different sets of cards. In the data.js file I have 2 arrays, one for cards in one language, and one for cards in another:

const cardsItalian = [{
    name: "Cane Bianco",
    image: "cane.png",
    id: "cane"
  },

  {
      name: "Cane Bianco",
      image: "cane2.png",
      id: "cane2"
    },

There is also a const cardsSpanish.

In the game.js file, I can call the data from data.js, and I have the following at the bottom:

const startGame = (cards, level) => {

// reset game variables
    gameStarted = false;
    moves = 0;
    matches = 0;
    setLevel(level);

    // reset HTML
    $('#game-board').empty();

    $(".clock").text('0:00');
    $("#moves").text('0');
    $('#winModal').hide();

    // Get cards and start the game
    let cardArray = makeCardArray(cardsItalian, level);

    shuffle(cardArray);
    displayCards(cardArray);

  };

This code allows me to take the cards from cardsItalian array, or I can change it to cardsSpanish, however I am struggling to figure out how to do it for one or the other based on the user's selection! Driving me nuts.

I'm sure I could add a condition but I'm not sure where

You could try using prompt .

var cards = prompt("Please select set. Type 1 for Italian or 2 for Spanish", "1 or 2");

if (cards ==1) 
let cardArray = makeCardArray(cardsItalian, level);
else if (cards ==2) 
let cardArray = makeCardArray(cardsSpanish, level);
else
//exit or call the function again till valid entry is made in prompt

Warning :

Prompt creates dialogue box and will not show up if the user has blocked dialogue box from appearing on the webpage. When dialogue box appears too often, browsers provide an option to disable them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM