Skip to content
Merged
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
32 changes: 9 additions & 23 deletions exercises/secret-handshake/example.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
export default function (handshake) {
const HANDSHAKE_COMMANDS = ['wink', 'double blink', 'close your eyes', 'jump', 'REVERSE'];
const handshakeCommands = ['wink', 'double blink', 'close your eyes', 'jump'];

export const secretHandshake = (handshake) => {
if (typeof handshake !== 'number') {
throw new Error('Handshake must be a number');
}

this.commands = () => this.shakeWith;
const shakeWith = handshakeCommands.filter((_, i) => (
handshake & (Math.pow(2, i))
));

this.calculateHandshake = (handshake) => {
const shakeWith = [];
if (handshake & (Math.pow(2, 4)))
shakeWith.reverse();

for (let i = 0; i < HANDSHAKE_COMMANDS.length; i++) {
const currentCommand = HANDSHAKE_COMMANDS[i];
const handshakeHasCommand = handshake & Math.pow(2, i);

if (handshakeHasCommand) {
if (currentCommand === 'REVERSE') {
shakeWith.reverse();
} else {
shakeWith.push(HANDSHAKE_COMMANDS[i]);
}
}
}

return shakeWith;
};

this.shakeWith = this.calculateHandshake(handshake);
}
return shakeWith;
};
26 changes: 9 additions & 17 deletions exercises/secret-handshake/secret-handshake.spec.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
import SecretHandshake from './secret-handshake';
import { secretHandshake } from './secret-handshake';

describe('Secret Handshake', () => {
test('binary 1 (hexadecimal 0x01) is a wink', () => {
const handshake = new SecretHandshake(0x01);
expect(handshake.commands()).toEqual(['wink']);
expect(secretHandshake(0x01)).toEqual(['wink']);
});

xtest('binary 10 (hexadecimal 0x02) is a double blink', () => {
const handshake = new SecretHandshake(0x02);
expect(handshake.commands()).toEqual(['double blink']);
expect(secretHandshake(0x02)).toEqual(['double blink']);
});

xtest('binary 100 (hexadecimal 0x04) is close your eyes', () => {
const handshake = new SecretHandshake(0x04);
expect(handshake.commands()).toEqual(['close your eyes']);
expect(secretHandshake(0x04)).toEqual(['close your eyes']);
});

xtest('binary 1000 (hexadecimal 0x08) is jump', () => {
const handshake = new SecretHandshake(0x08);
expect(handshake.commands()).toEqual(['jump']);
expect(secretHandshake(0x08)).toEqual(['jump']);
});

xtest('binary 11 (hexadecimal 0x03) is wink and double blink', () => {
const handshake = new SecretHandshake(0x03);
expect(handshake.commands()).toEqual(['wink', 'double blink']);
expect(secretHandshake(0x03)).toEqual(['wink', 'double blink']);
});

xtest('binary 10011 (hexadecimal 0x13) is double blink and wink', () => {
const handshake = new SecretHandshake(0x13);
expect(handshake.commands()).toEqual(['double blink', 'wink']);
expect(secretHandshake(0x13)).toEqual(['double blink', 'wink']);
});

xtest('binary 11111 (hexadecimal 0x1F) is jump, close your eyes, double blink, and wink', () => {
const handshake = new SecretHandshake(0x1F);
expect(handshake.commands()).toEqual(['jump', 'close your eyes', 'double blink', 'wink']);
expect(secretHandshake(0x1F)).toEqual(['jump', 'close your eyes', 'double blink', 'wink']);
});

xtest('text is an invalid secret handshake', () => {
expect(() => new SecretHandshake('piggies'))
expect(() => secretHandshake('piggies'))
.toThrow(new Error('Handshake must be a number'));
});
});