Avatar of brunnock

brunnock's solution

to Secret Handshake in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

There are 10 types of people in the world: Those who understand binary, and those who don't.

You and your fellow cohort of those in the "know" when it comes to binary decide to come up with a secret "handshake".

1 = wink
10 = double blink
100 = close your eyes
1000 = jump


10000 = Reverse the order of the operations in the secret handshake.

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the input 3, the function would return the array ["wink", "double blink"] because 3 is 11 in binary.

Given the input 19, the function would return the array ["double blink", "wink"] because 19 is 10011 in binary. Notice that the addition of 16 (10000 in binary) has caused the array to be reversed.

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine secret-handshake.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

Bert, in Mary Poppins http://www.imdb.com/title/tt0058331/quotes/qt0437047

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

secret-handshake.spec.js

var SecretHandshake = require('./secret-handshake');

describe('Secret Handshake', function () {
  it('1 is a wink', function () {
    var handshake = new SecretHandshake(1);
    expect(handshake.commands()).toEqual(['wink']);
  });

  xit('10 is a double blink', function () {
    var handshake = new SecretHandshake(2);
    expect(handshake.commands()).toEqual(['double blink']);
  });

  xit('100 is close your eyes', function () {
    var handshake = new SecretHandshake(4);
    expect(handshake.commands()).toEqual(['close your eyes']);
  });

  xit('1000 is jump', function () {
    var handshake = new SecretHandshake(8);
    expect(handshake.commands()).toEqual(['jump']);
  });

  xit('11 is wink and double blink', function () {
    var handshake = new SecretHandshake(3);
    expect(handshake.commands()).toEqual(['wink', 'double blink']);
  });

  xit('10011 is double blink and wink', function () {
    var handshake = new SecretHandshake(19);
    expect(handshake.commands()).toEqual(['double blink', 'wink']);
  });

  xit('11111 is jump, close your eyes, double blink, and wink', function () {
    var handshake = new SecretHandshake(31);
    expect(handshake.commands()).toEqual(['jump', 'close your eyes', 'double blink', 'wink']);
  });

  xit('text is an invalid secret handshake', function () {
    expect( function () {
      /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/
      var ignoredHandshake = new SecretHandshake('piggies');
    }).toThrow(new Error('Handshake must be a number'));
  });
});
function SecretHandshake(code){
  if (isNaN(code)) throw ('Handshake must be a number');

  var words=['wink', 'double blink', 'close your eyes', 'jump'];

  this.commands = function(){
    var translation=[];
    for (var x=0; x<4; ++x) 
      if (code & Math.pow(2,x)) translation.push(words[x]);
    if (code>16) translation.reverse();
    return translation;
  }
}

module.exports=SecretHandshake;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?