Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function memoryGame(input) {
- let sequance = input.shift().split(' ');
- let movesCount = 0;
- let command = input.shift();
- let sequanceLength = sequance.length;
- while (command !== 'end') {
- let indexes = command.split(' ');
- indexes.sort((a, b) => b - a);
- movesCount++;
- let firstIndex = Number(indexes[0]);
- let secondIndex = Number(indexes[1]);
- if (
- firstIndex === secondIndex ||
- firstIndex < 0 ||
- secondIndex < 0 ||
- firstIndex > sequanceLength - 1 ||
- secondIndex > sequanceLength - 1
- ) {
- let startingIndex = sequanceLength / 2 - 1;
- let newElements = `-${movesCount}a`;
- sequance.splice(startingIndex, 0, newElements, newElements);
- console.log(`Invalid input! Adding additional elements to the board`);
- } else if (sequance[firstIndex] === sequance[secondIndex]) {
- let element = sequance.splice(Number(firstIndex), 1);
- sequance.splice(Number(secondIndex), 1);
- console.log(`Congrats! You have found matching elements - ${element}!`);
- } else if (sequance[firstIndex] !== sequance[secondIndex]) {
- console.log(`Try again!`);
- }
- command = input.shift();
- if (sequance.length <= 1) {
- console.log(`You have won in ${movesCount} turns!`);
- break;
- } else if (sequance.length > 0 && command == 'end') {
- console.log(`Sorry you lose :(`);
- console.log(sequance.join(' '));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment