Weekly Code Puzzle #4 Results: "Dreidel Dreidel"

Happy holidays folks! As mentioned last week, we chose a holiday-themed code challenge, 'Dreidel Dreidel.' The challenge was to implement a gambling game with a four-sided Dreidel using JavaScript. You had to write a function that takes an array of the sides rolled, the total number of coins you have, and the total pot. The function needed to understand the rules of each side and return your new amount of coins. You can get more details at the challenge here.

The "Who's On First?" Award

Bloc engineer Ariel Fogel narrowly prevented mentor Brian Bugh from attaining this title two weeks in a row:

function gamble(rolls, myCoins, pot) {  
   rolls.forEach((roll) => {
     let rollOutcome = rules(roll, myCoins, pot);
     myCoins += rollOutcome;
     pot -= rollOutcome;
   })
   return myCoins;
}

function rules(letter, myCoins, pot) {  
  let returnCoins = 0;
  switch(letter) {
    case 'Shin':
      returnCoins = -1;
      break;
    case 'Hei':
      returnCoins = Math.floor(pot * 0.5);
      break;
    case 'Gimel':
      returnCoins = pot;
      break;
    default:
      break;
  }
  return returnCoins;
}

Ariel uses the ol' switcheroo (JavaScript switch statement).

This was a popular choice for many of the solutions to this puzzle.

The "Risky Business" Award

Bloc Curriculum Developer Aaron Brager gets this award for using JavaScript's eval() function, which evaluates code from a string.

function gamble(rolls, myCoins, pot) {  
  var bank = [myCoins, pot];
  for (i = 0; i < rolls.length; i++) {
    bank = eval(rolls[i].toLowerCase())(bank[0], bank[1]);
  }
  return bank[0];
}

function nun(myCoins, pot) { return [myCoins, pot]; }  
function gimel(myCoins, pot) { return [myCoins + pot, 0]; }  
function hei(myCoins, pot) { var coins = Math.floor(pot / 2); return [myCoins + coins, pot - coins]; }  
function shin(myCoins, pot) { return [myCoins - 1, pot + 1]; }  

The Mozilla Developer Network considers this rather dangerous, because if this function is provided input from a third party, it's susceptible to running malicious code.

Still, it's an interesting feature of JavaScript that Aaron demonstrates.

The "Taylor Swift" Award

Bloc alum and iOS Developer Chris Slowik gets this award for implementing a solution, not in JavaScript, but Apple's new Swift programming language.

You can see the similarities between Swift and JavaScript at play here, from the similar func declaration down to the switch statements.

The "I Don't Mean To Brag But I Was Pretty Clever" Award

Goes to the author of this post, for managing to get a 'clever' vote on Codewars.

function gamble(rolls, myCoins, pot) {  
  var dreidel = {
    Nun: function() {
      return;
    },
    Gimel: function() {
      myCoins += pot;
    },
    Hei: function() {
      myCoins += (pot = Math.floor(pot / 2));
    },
    Shin: function() {
      pot += 1;
      myCoins -= 1; 
    }
  };

  rolls.forEach(function(roll) {
    dreidel[roll]();
  });

  return myCoins;
}

Rather than use a switch statement, I created an Object whose properties are the sides of the dreidel and the values are functions implementing the rules for each side. This way, I iterate over the given sides and use JavaScript's bracket notation to call the appropriate function.

See you next year!

Because most people will be engaging in holiday cheer, we'll hold off on posting a new challenge for now. Let us know if you have a suggestion for the next challenge at weeklypuzzle@bloc.io or tweet it at @BlocPuzzle.