Skip to content

Instantly share code, notes, and snippets.

@fcarlone
Last active May 18, 2018 03:45
Show Gist options
  • Save fcarlone/842488c95aa6036be09dac3ea89c44f7 to your computer and use it in GitHub Desktop.
Save fcarlone/842488c95aa6036be09dac3ea89c44f7 to your computer and use it in GitHub Desktop.
Memory Game Board Generator
<div class='container-fluid language-javascript' >
<!-- tabs -->
<ul class="nav nav-pills">
<li class="active">
<a href="#readme" data-toggle="tab">Readme</a>
</li>
<li>
<a href="#specs" data-toggle="tab">Specs</a>
</li>
</ul>
<!-- tab content -->
<div class="tab-content clearfix" style="margin-top: 20px">
<div class="tab-pane" id="specs">
</div>
<div class="tab-pane active" id="readme">
</div>
</div>
</div>
<script type="text/markdown" id="readme-md">
## Challenge of the Week (COTW)
**Week of: 5/11 - 5/18 - Review: Friday, 5/18 @ 1:30 PM**
Welcome to the challenge of the week - Review the problem below, then write your JS code in the JS editor, then click run and check the specs tab. We'll be livestreaming the solution <a target ='_blank' href= "https://www.facebook.com/events/174828736669011/">here every Friday at 1:30pm ET</a>.
<hr>
<br>
### Memory Game Board Generator
Let's create a playing board for a basic memory game! The memory game has a board with numbered cards, there is a pair of each number, and all the numbers are randomly placed on the board. For example, the board may contain `[1,2,1,3,3,2]`. The active player gets two guesses, the goal of the game is to guess the locations of the same number. If the player guesses success, they are rewarded with two more guesses. The game ends when all the numbers are guessed and removed from the board. The winner is the player with the most successful guesses.
Create the function `memoryBoardSetup` that takes a number as an argument and returns an array of numbers. The array should contain a pair of the same numbers (only one pair for a number), and the length of the array should match the number argument passed to the function. The numbers in the array can't be larger than the size of the board (the number argument). If the number argument is 0 or an odd number, return an empty array. The array of pairs should be shuffled (in random order).
```js
// Note: The arrays are randomized each function invocation, different numbers may be in different locations, the length of the
// array and only one pair of a number is allowed. The length is the same size as the number argument. No number in the array can
// be higher than the number argument and 0 should not be included in the array.
memoryBoardSetup(24); // returned [19, 14, 3, 16, 17, 12, 4, 23, 16, 2, 14, 22, 22, 15, 23, 19, 4, 3, 2, 15, 7, 12, 7, 17];
memoryBoardSetup(6); // [ 5, 4, 4, 2, 5, 2 ];
memoryBoardSetup(6); // [ 6, 6, 2, 2, 5, 5 ];
memoryBoardSetup(6); // [ 3, 3, 6, 1, 1, 6 ];
memoryBoardSetup(6); // [ 5, 6, 4, 6, 4, 5 ];
// When the number argument is 0 or odd, the array returned is empty
memoryBoardSetup(0); // [];
memoryBoardSetup(11); // [];
memoryBoardSetup(239)j; // [];
```
### Tips:
- Review the [Math.random()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) and rounding functions such as [Math.ceil](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil) or [Math.floor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) as examples.
- The length of the array returned is the same as the number argument
- If the number argument is 0 or an odd number, return an empty array
- The array returned should contain pairs of numbers in random order
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.12.2/themes/prism.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.0.0/jasmine-html.js"></script>
<!-- this is a custom boot that obeys a getContainer function on window -->
<script type="text/javascript">
window.getContainer = function() {
return document.getElementById("specs");
}
</script>
<script src="https://cdn.rawgit.com/davidyang/f63658195de7607f476b41a8f14c2096/raw/d7628b8cff6da10385bbc96ecf205bf47e2ca4a4/custom-boot.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.17/marked.js"></script>
<script>
// insert the readme as Markdown into the template
var data = document.getElementById("readme-md").innerHTML;
document.getElementById("readme").innerHTML = marked(data)
</script>
var memoryBoardSetup = function (num) {
var result = [];
var n = 0;
if (num % 2 !== 0 || num === 0) {
return [];
}
while(result.length < num) {
var randNum = (Math.floor(Math.random() * num) + 1);
if (!result.includes(randNum)) {
result.push(randNum, randNum);
}
n++;
}
// shuffle array
function shuffleArray(array) {
for (var i = (array.length-1); i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
return shuffleArray(result);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment