Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function memoryGame(input) {
- let game = input[0].split(' ');
- for (let i = 1; i < input.length - 1; i++) {
- [index1, index2] = input[i].split(' ').map(Number).sort();
- if (index1 >= 0 && index1 < game.length && index2 >= 0 && index2 < game.length && index1 != index2) {
- if (game[index1] === game[index2]) {
- console.log(`Congrats! You have found matching elements - ${game[index1]}!`);
- game.splice(index2, 1);
- game.splice(index1, 1);
- if (game.length == 0) {
- console.log(`You have won in ${i} turns!`);
- return;
- }
- } else {
- console.log("Try again!");
- }
- } else {
- game.splice(game.length / 2, 0, `-${i}a`, `-${i}a`);
- console.log("Invalid input! Adding additional elements to the board");
- }
- }
- console.log(`Sorry you lose :(\n${game.join(" ")}`);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement