Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 293.flip-game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* URL of this problem
* https://leetcode.com/problems/flip-game/description/
*
* @param {string} currentState
* @return {string[]}
*/

var generatePossibleNextMoves = function(currentState) {
const PossibleStates = [];

for (let i = 0; i < currentState.length - 1; i++) {
const CurrentStateArr = [...currentState];

if (CurrentStateArr[i] === "+" && CurrentStateArr[i + 1] === "+") {
CurrentStateArr[i] = "-";
CurrentStateArr[i + 1] = "-";

PossibleStates.push(CurrentStateArr.join(""));
}
}

return PossibleStates;
};

module.exports = generatePossibleNextMoves;
17 changes: 17 additions & 0 deletions generatePossibleNextMoves.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const generatePossibleNextMoves = require("./293.flip-game");

test("Return an array of all the possible states after one valid move", () => {
expect(generatePossibleNextMoves("++++")).toEqual(["--++","+--+","++--"]);
});

test("Return an empty array if the input currentState is made of only one of '+'", () => {
expect(generatePossibleNextMoves("+")).toEqual([]);
});

test("Return an empty array if the input currentState is an empty string", () => {
expect(generatePossibleNextMoves("")).toEqual([]);
});

test("Return an empty array if the input currentState comprises only '-", () => {
expect(generatePossibleNextMoves("----")).toEqual([]);
});