↓ Skip to main content
Life

Handshake: JS

Secret Handshake
#

Objective was relatively simple.

  • A numeral will be provided as input.
  • Based on the binary equivalent, one needs to compute the secret handshake.

Rule for handshake.

1 = wink 10 = double blink 100 = close your eyes 1000 = jump 10000 = Reverse the order of the operations in the secret handshake.


## Example

Let’s say the input is 2 the output should be [“double blink”].

## Version 1.0

```text
function binaryOptimized(number) {         
     var binaryRep = [];                 
     do {            
         binaryRep.push(parseInt(number%2));             
         number /= 2;          
    } while (parseInt(number) > 0);               
    return binaryRep;                     
}

function getCommand(pseudo, arr) {       switch(pseudo) {                  
case 1:   arr.push ("wink");  break;               
case 10:  arr.push ("double blink"); break;            
case 100:  arr.push ("close your eyes"); break;           
case 1000:  arr.push ("jump");   break;            
case 10000:  arr = arr.reverse();         
}                        
return arr;                      
} 
                              
export const commands = (input) => {       
    var rep = binaryOptimized(input);         
    var commands = [];            
    rep.map((element, i) => {            
           getCommand(element * Math.pow(10, i), commands)              
    });               
    return commands;               
};

The code is self explanatory

Defined a binary calculation function, computed the equivalent binary representation. Used the individual element to compute the corresponding command and responded back with the response.

Can it be optimized?
#

The think I misinterpreted was that, binary calculation of number is an optional thing.

BITWISE Operator
#

There are two basic bitwise operation ‘&’ and ‘|’.

Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers.

Example
#

5 => 101
We can check the bits now
2^0 = 1 => 1
2^1 = 2 => 10
2^2 = 4 => 100
2^3 = 8 => 1000
2^4 = 16 => 10000
2^5 = 32 => 100000

Now instead of converting it to binary I can simply & to compute the on bits.

5&1  = 101 & 1 => 1
5&2 = 101 & 10 => 0
5&4 = 101 & 100 => 4 
5&8 = 101 & 1000 => 0
5&16 = 101 & 10000 => 0
5&32 = 101 & 100000 => 0

Also you can define the left shift to compute the bit rep.

1<<0 = 1
1<<1 = 10 = 2
1<<2 = 100 = 4
1<<3 = 1000 = 8
1<<4 = 10000 = 16
1<<5 = 100000 = 32

So to compute the secret handshake it is just matter of computing the on bits and add it to the final array.

I defined the Array of handshakes

HANDSHAKES = ["wink", "double blink", "close your eyes", "jump"];

To compute the handshake sequence I just to validate the ON bits.

Array.filter
#

let newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

Documentation is available here.

Now utilizing the left shift operator, and bitwise & it was easy to compute the final array of handshakes.

HANDSHAKES.filter( (signal, index) => n & (1 << index));

Where n is the input number from user.

Illustration
#

If n = 3 then,

HANDSHAKES.filter() will Execute from 0..3

3 = (11)BASE2

Now, 

[], 0 => 3&1, ["wink"]
[], 1 => 3&2, ["wink", "double blink"]
[], 2 => 3&4, ["wink", "double blink"]
[], 3 => 3&8, ["wink", "double blink"]

For the last condition to reverse we can employ

if (n & (1 << HANDSHAKES.length)) {
    result.reverse();
}

For n = 3, above condition evaluates false so the final result comes out as

[“wink”, “double blink”]

const HANDSHAKES = ["wink", "double blink", "close your eyes", "jump"];

export const commands = (n) => {
  if (typeof n !== "number") {
    throw new Error("Handshake must be a number");
  }  
  const result = HANDSHAKES.filter((signal, index) => n & (1 << index));

  if (n & (1 << HANDSHAKES.length)) {
    result.reverse();
  }

  return result;
};

— THE – END —

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Asynchronous

·1 min
I learned a whole new dimension to this word, quite recently. Usually this word is used to express the non-blocking way of calling a program, a service or a code. Who would have thought it can be used for management or product development style as well.

Elastic 7.5

·1 min
Installing # Step 1: Using docker # yum install docker docker pull docker.elastic.co/elasticsearch/elasticsearch:7.5.1 systemctl list-units --type=service docker version Client: Version: 1.13.1 API version: 1.26 Package version: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? sudo systemctl start docker docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64 Go version: go1.10.3 Git commit: 7f2769b/1.13.1 Built: Sun Sep 15 14:06:47 2019 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64 Go version: go1.10.3 Git commit: 7f2769b/1.13.1 Built: Sun Sep 15 14:06:47 2019 OS/Arch: linux/amd64 Experimental: false docker pull docker.elastic.co/elasticsearch/elasticsearch:7.5.1 Trying to pull repository docker.elastic.co/elasticsearch/elasticsearch ... 7.5.1: Pulling from docker.elastic.co/elasticsearch/elasticsearch c808caf183b6: Pull complete 05ff3f896999: Pull complete 82fb7fb0a94e: Pull complete c4d0024708f4: Pull complete 136650a16cfe: Pull complete 968db096c092: Pull complete 42547e91692f: Pull complete Digest: sha256:b0960105e830085acbb1f9c8001f58626506ce118f33816ea5d38c772bfc7e6c Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:7.5.1 docker pull docker.elastic.co/kibana/kibana:7.5.1