Basic Nim game vs computer in JavaScript This is my first time writing a basic program/game in any programming language.
It is supposed to a be a Nim game vs a computer with these basic parameters:


Use prompt() to prompt moves and input.
Use console.log() draw the board.


The board should look like this:


  Pile A: ooooo
  
  Pile B: ooo
  
  Pile C: ooooo


It has been very difficult for me to approach this problem, and I know the logic that I have used is very basic and buggy. Any pointers would be appreciated, whether in the code or my approach to solving the problem in general.

 var pileA = {
   circles: "ooooo",
   print: function() {
     console.log("Pile A: " + this.circles);
   }
 };

 var pileB = {
   circles: "ooooo",
   print: function() {
     console.log("Pile B: " + this.circles);
   }
 };

 var pileC = {
   circles: "ooooo",
   print: function() {
     console.log("Pile C: " + this.circles);
   }
 };

 function printBoard(){
     console.log("**** Nim ****");
     pileA.print();
     pileB.print();
     pileC.print();
 }

 function yourMove(){

     printBoard();

     var moveData = prompt("Type letter of pile and # of o's you are removing.\n" +
                             "Ex. A1 or C4");

     if (moveData[0] == 'A') {
         var x = pileA.circles.length - moveData[1];
         pileA.circles = pileA.circles.slice(0,x);
         return pileA.circles;
     } else if (moveData[0] == 'B') {
         var x = pileB.circles.length - moveData[1];
         pileB.circles = pileB.circles.slice(0,x);
         return pileB.circles;
     } else if (moveData[0] == 'C') {
         var x = pileC.circles.length - (moveData[1]+1);
         pileC.circles = pileC.circles.slice(0,x);
         return pileC.circles;
         }

 }

 function computersMove() {

     printBoard();

     alert("Now it is the computer's turn!");

     if (pileA.circles != "") {
         pileA.circles = pileA.circles.slice(0,0);
         return pileA.circles;
     } else if (pileB.circles != "") {
         pileB.circles = pileB.circles.slice(0,0);
         return pileB.circles;
     } else if (pileC.circles != "") {
         pileC.circles = pileC.circles.slice(0,0);
         return pileC.circles;
     }

     if (pileA.circles == "" &amp;&amp;
     pileB.circles == "" &amp;&amp;
     pileC.circles == "") {
         console.log("Computer Wins!!!");
         }
 }

 while (pileA.circles != "" || 
     pileB.circles != "" ||
     pileC.circles != "") {

         yourMove();
         if (pileA.circles == "" &amp;&amp;
             pileB.circles == "" &amp;&amp;
             pileC.circles == "") {
                 console.log("You win!!!");
                 }

        if (pileA.circles != "" || 
            pileB.circles != "" ||
            pileC.circles != "") { 
                 computersMove();
                 }
     }

 alert('Game Over!');
I stumbled across this because a friend told me he got hit with this question recently for a coding bootcamp entrance exam. I had to look and see what it is, of course : )

Josh has a great answer with excellent use of higher order functions and keeping things DRY. At a glance, one change that I'd make with his code is the usage of the reduce function. It's a bit verbose and possibly difficult for a beginner to parse through. A clearer declaration would be using something like Array.every (once all the circles return an empty string "", the GameOver function returns true). It's a tiny gripe, but it's more intuitive. The usage would be substituted like this:

function isGameOver() {
  return piles.every(function(pile) {
    return pile.circles === "";
  });
}