Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. const payPad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [null, 0, null]];
  2. function getIndexes(n) {
  3. let arrayIndex = Math.floor((n - 1) / 3);
  4. let numberIndex = n - 1 - 3 * arrayIndex;
  5. return [arrayIndex, numberIndex];
  6. }
  7.  
  8. function getPossibilies([arrayIndex, numberIndex]) {
  9. const resullt = [];
  10. for (let i = 0; i <= 3; i++) {
  11. if (arrayIndex - i === 0) {
  12. for (let j = 0; j < 3; j++) {
  13. if (Math.abs(numberIndex - j) < 2) {
  14. resullt.push(payPad[i][j]);
  15. }
  16. }
  17. }
  18. if (Math.abs(i - arrayIndex) == 1) {
  19. resullt.push(payPad[i][numberIndex]);
  20. }
  21. }
  22. return resullt;
  23. }
  24.  
  25. function getPins(observed) {
  26. const result = [];
  27. const possibilities = [];
  28. observed.split("").forEach(n => {
  29. possibilities.push(getPossibilies(getIndexes(Number(n))));
  30. });
  31. for (let i = 0; i < possibilities[0].length; i++) {
  32. let str = `${possibilities[0][i]}`;
  33. for (let j = 1; j < possibilities.length; j++) {
  34. for (let k = 0; k < possibilities[j].length; k++) {
  35. str += possibilities[j][k];
  36. if (str.length == observed.length) {
  37. result.push(str);
  38. str = `${possibilities[0][i]}`;
  39. }
  40. }
  41. }
  42. }
  43. return result;
  44. }
  45.  
  46. getPins("11");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement