Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Board indexes:
- // 0 | 1 | 2
- // ---+---+---
- // 3 | 4 | 5
- // ---+---+---
- // 6 | 7 | 8
- const getRowNumGivenWidth = w => i => Math.floor(i/w);
- // I want to be able to declare nextIndexInRowGivenWidth() here, outside of main()
- // but getRowNum() needs to be defined beforehand
- const main = () => {
- // User input:
- const width = 3;
- // ...
- const getRowNum = getRowNumGivenWidth(width);
- const nextIndexInRowGivenWidth = width => currentIndex => {
- const rowNum = getRowNum(currentIndex);
- const nextIndex = currentIndex + 1;
- if (getRowNum(nextIndex) != rowNum)
- result = nextIndex - width;
- else
- result = nextIndex;
- return result;
- };
- const nextIndexInRow = nextIndexInRowGivenWidth(width);
- const board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
- board.map(x => console.log(x, " -> ", nextIndexInRow(x)));
- // ...
- }
- main();
Add Comment
Please, Sign In to add comment