Let's discuss how we will implement our project.
I would start by making an array that will store the cities that our players enter:
let cities = [];
Suppose we have the beginning of the game
and the first player moves. He inputs a
city and presses Enter
. Obviously,
we can immediately add this city to our
array with cities.
The second player will now take a turn. Since our array is no longer empty, this second move will be described by the same algorithm as all subsequent moves.
What is this algorithm? First, you need to check that the first letter of the entered city matches the last letter of the previous city. Secondly, you need to check that such a city has not yet been in this game.
If both of these conditions are met, then the entered city is written to our array and the move goes to the next player. If some condition is not met, then you need to display a message about it.
Implement the game according to the described algorithm.