From 3b79752b86aee9602fe81302ae9c0938a52b64cd Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 19 May 2016 09:40:22 +0200 Subject: [PATCH] Validate memory card params before use HFJ-1914 --- card.js | 13 +++++++++++++ memory-game.js | 12 +++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/card.js b/card.js index 0c0a608..dca1639 100644 --- a/card.js +++ b/card.js @@ -99,4 +99,17 @@ MemoryGame.Card.prototype = Object.create(EventDispatcher.prototype); MemoryGame.Card.prototype.constructor = MemoryGame.Card; + /** + * Check to see if the given object corresponds with the semantics for + * a memory game card. + * + * @param {object} params + * @returns {boolean} + */ + MemoryGame.Card.isValid = function (params) { + return (params !== undefined && + params.image !== undefined && + params.image.path !== undefined); + }; + })(H5P.MemoryGame, H5P.EventDispatcher, H5P.jQuery); diff --git a/memory-game.js b/memory-game.js index 98166b8..0f92b5e 100644 --- a/memory-game.js +++ b/memory-game.js @@ -101,11 +101,13 @@ H5P.MemoryGame = (function (EventDispatcher, $) { // Initialize cards. for (var i = 0; i < parameters.cards.length; i++) { - // Add two of each card - var cardOne = new MemoryGame.Card(parameters.cards[i], id); - var cardTwo = new MemoryGame.Card(parameters.cards[i], id); - addCard(cardOne, cardTwo); - addCard(cardTwo, cardOne); + if (MemoryGame.Card.isValid(parameters.cards[i])) { + // Add two of each card + var cardOne = new MemoryGame.Card(parameters.cards[i], id); + var cardTwo = new MemoryGame.Card(parameters.cards[i], id); + addCard(cardOne, cardTwo); + addCard(cardTwo, cardOne); + } } H5P.shuffleArray(cards);