didkoslawow

Untitled

Jan 5th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. function memoryGame(input) {
  2. let sequance = input.shift().split(' ');
  3. let movesCount = 0;
  4. let command = input.shift();
  5. let sequanceLength = sequance.length;
  6.  
  7. while (command !== 'end') {
  8. let indexes = command.split(' ');
  9. indexes.sort((a, b) => b - a);
  10. movesCount++;
  11. let firstIndex = Number(indexes[0]);
  12. let secondIndex = Number(indexes[1]);
  13.  
  14. if (
  15. firstIndex === secondIndex ||
  16. firstIndex < 0 ||
  17. secondIndex < 0 ||
  18. firstIndex > sequanceLength - 1 ||
  19. secondIndex > sequanceLength - 1
  20. ) {
  21. let startingIndex = sequanceLength / 2 - 1;
  22. let newElements = `-${movesCount}a`;
  23. sequance.splice(startingIndex, 0, newElements, newElements);
  24. console.log(`Invalid input! Adding additional elements to the board`);
  25. } else if (sequance[firstIndex] === sequance[secondIndex]) {
  26. let element = sequance.splice(Number(firstIndex), 1);
  27. sequance.splice(Number(secondIndex), 1);
  28. console.log(`Congrats! You have found matching elements - ${element}!`);
  29. } else if (sequance[firstIndex] !== sequance[secondIndex]) {
  30. console.log(`Try again!`);
  31. }
  32.  
  33. command = input.shift();
  34. if (sequance.length <= 1) {
  35. console.log(`You have won in ${movesCount} turns!`);
  36. break;
  37. } else if (sequance.length > 0 && command == 'end') {
  38. console.log(`Sorry you lose :(`);
  39. console.log(sequance.join(' '));
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment