Advertisement
ErolKZ

Untitled

Oct 21st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. function solve(arr) {
  2.  
  3. let sequence = arr.shift().split(' ');
  4.  
  5. let i = 0;
  6.  
  7.  
  8. while (arr[i] !== 'end') {
  9.  
  10. let arr2 = arr[i].split(' ').map(Number);
  11.  
  12. let index1 = Number(arr2[0]);
  13.  
  14. let index2 = Number(arr2[1]);
  15.  
  16.  
  17. if (index1 === index2 || index1 < 0 || index1 > sequence.length - 1 || index2 < 0 || index2 > sequence.length - 1) {
  18.  
  19. let cenOfSeq = sequence.length / 2;
  20.  
  21. sequence.splice(cenOfSeq, 0, `-${i + 1}a`);
  22.  
  23. sequence.splice(cenOfSeq, 0, `-${i + 1}a`);
  24.  
  25. console.log(`Invalid input! Adding additional elements to the board`);
  26.  
  27.  
  28. } else if (sequence[index1] === sequence[index2]) {
  29.  
  30. console.log(`Congrats! You have found matching elements - ${sequence[index1]}!`);
  31.  
  32. let indexHolder = sequence[index1];
  33.  
  34. sequence = sequence.filter(el => el !== indexHolder);
  35.  
  36. if (sequence.length === 1) {
  37.  
  38. sequence = [];
  39.  
  40. }
  41.  
  42.  
  43. } else {
  44.  
  45. console.log(`Try again!`);
  46.  
  47. }
  48.  
  49. if (sequence.length === 0) {
  50.  
  51. console.log(`You have won in ${i + 1} turns!`);
  52.  
  53. return;
  54.  
  55. }
  56.  
  57. i++;
  58. }
  59.  
  60.  
  61. console.log(`Sorry you lose :(
  62. ${sequence.join(' ')}`);
  63.  
  64.  
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement