Guest User

Untitled

a guest
May 25th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // Board indexes:
  2. // 0 | 1 | 2
  3. // ---+---+---
  4. // 3 | 4 | 5
  5. // ---+---+---
  6. // 6 | 7 | 8
  7.  
  8. const getRowNumGivenWidth = w => i => Math.floor(i/w);
  9.  
  10. // I want to be able to declare nextIndexInRowGivenWidth() here, outside of main()
  11. // but getRowNum() needs to be defined beforehand
  12.  
  13.  
  14. const main = () => {
  15.  
  16. // User input:
  17. const width = 3;
  18.  
  19. // ...
  20.  
  21.  
  22. const getRowNum = getRowNumGivenWidth(width);
  23.  
  24. const nextIndexInRowGivenWidth = width => currentIndex => {
  25. const rowNum = getRowNum(currentIndex);
  26. const nextIndex = currentIndex + 1;
  27.  
  28. if (getRowNum(nextIndex) != rowNum)
  29. result = nextIndex - width;
  30. else
  31. result = nextIndex;
  32.  
  33. return result;
  34. };
  35.  
  36.  
  37. const nextIndexInRow = nextIndexInRowGivenWidth(width);
  38.  
  39. const board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
  40.  
  41. board.map(x => console.log(x, " -> ", nextIndexInRow(x)));
  42.  
  43. // ...
  44.  
  45. }
  46.  
  47. main();
Add Comment
Please, Sign In to add comment