From e8e55213712c65e23dee55778fe297c31a35a77f Mon Sep 17 00:00:00 2001 From: ak Date: Mon, 17 Jul 2023 13:52:47 -0700 Subject: [PATCH] formatter function moved out for flow/legibility --- src/knightMoves.js | 53 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/knightMoves.js b/src/knightMoves.js index 3783349..c911784 100644 --- a/src/knightMoves.js +++ b/src/knightMoves.js @@ -1,5 +1,31 @@ import { gameBoard } from "./gameBoard.js"; +// formatter for final output +const format = (moves) => { + // fix the output array by traversing provided array of BFS search results + // get last square (the destination) + let square = moves[moves.length - 1]; + // loop through previous squares until there are no more previous squares (the origin) + // get array of path - made changeable for later + let arr = []; + while (square != null) { + arr.unshift(square); + square = square.prev; + } + // format the output string in the Odin-approved way + let output = `=> You made it in ${ + arr.length - 1 + } moves! Here's your path:\n`; + // loop through returned array of square objects to return array of positions and append them to the output + for (let i = 0; i < arr.length; i++) { + // change each array index to be the position of the current square object + arr[i] = new Array(arr[i].x, arr[i].y); + // then add it to the output string + output = `${output} [${arr[i]}]\n`; + } + return console.log(output); +}; + export const knightMoves = (origin, destination) => { // make board - 8 x 8 const board = gameBoard(8); @@ -14,31 +40,6 @@ export const knightMoves = (origin, destination) => { // possible move combinations for x and y - linked at index values - moves clockwise const possibleX = [1, 2, 2, 1, -1, -2, -2, -1]; const possibleY = [2, 1, -1, -2, -2, -1, 1, 2]; - // formatter for final output - const format = (moves) => { - // fix the output array by traversing provided array of BFS search results - // get last square (the destination) - let square = moves[moves.length - 1]; - // loop through previous squares until there are no more previous squares (the origin) - // get array of path - made changeable for later - let arr = []; - while (square != null) { - arr.unshift(square); - square = square.prev; - } - // format the output string in the Odin-approved way - let output = `=> You made it in ${ - arr.length - 1 - } moves! Here's your path:\n`; - // loop through returned array of square objects to return array of positions and append them to the output - for (let i = 0; i < arr.length; i++) { - // change each array index to be the position of the current square object - arr[i] = new Array(arr[i].x, arr[i].y); - // then add it to the output string - output = `${output} [${arr[i]}]\n`; - } - return console.log(output); - }; // initialize looping variables let current; const queue = [originSquare]; @@ -72,4 +73,4 @@ export const knightMoves = (origin, destination) => { } // if not found, return infinity return Infinity; -}; +}; \ No newline at end of file