EllyChanx / Hoover

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hoover

Hoover is a Node.js console application that follows the instructions from here.

Requirements

Hoover program processes input.txt that reside in the same directory of Hoovers.js and takes input as the following format:

  • the first line holds the room dimensions (X Y), separated by a single space
  • the second line holds the initial hoover coordinates
  • subsequent lines contain the zero or more positions of patches of dirt (one per line)
  • the next line then always contains the hoover's move instructions (at least one)

The goal of the program is to take the above information then output the following on the terminal:

  • The final hoover position (X, Y)
  • The number of patches of dirt the robot cleaned up

Installation

To install dependencies, clone the repo to a machine with Node installed and run:

npm install

Usage

Ensure input.txt is in the same folder as Hoover.js and is populated with desired input. At the root directory of the program and run:

node Hoover.js

or

npm start

Testing and Coverage

Unit tests were written in Jasmine v3.3.1 and Jest v24.5.0 for edge cases. To use jasmine and jest commands, the following packages are required to be installed globally:

npm install jasmine-node jasmine jest -g

To run tests and see coverage, enter:

jasmine; jest
jest --coverage

or

npm test -- --coverage

To see linting result, enter:

eslint Hoover.js

Approach

  • Function development were aimed to follow the single responsibility principle. In this way, logic should be relatively easier to test and maintain.
  • A .outputResult function is applied to integrate the logic and log the desirable output. In case of other parameters are required on the output, produce it in .outputResult can avoid tinkering on the logic. This function has no unit test.
  • Function names start with .get (e.g. .getRoomSize & .getInitialCoord) are designed to process raw data and arrange them into workable arrays.

Assumptions

  1. Multiple patches of dirt on the same coordinate should all be cleaned up by Hoover and counted individually even Hoover only clean such coordinate once.
  2. Hoover should not be placed outside of the room; otherwise, an error will be logged.
  3. When Hoover is at the coordinate of any walls, further move instructions into the wall should not take effect; Hoover should stay in place until the next effective instruction take place.

Improvements

  1. The move instructions (cardinal directions) are being processed into an array this.moves then iterated with a switch:
self.moves.map( i => {
  switch (i) {
    case "N":
      Y + 1 > self.roomHeight ? true : self.moveTrace.push([`${X}`, `${Y+=1}`]);
      break;
    case "E":
      X + 1 > self.roomWidth ? true : self.moveTrace.push([`${X+=1}`, `${Y}`]);
      break;
    case "S":
      Y - 1 < 0 ? true : self.moveTrace.push([`${X}` , `${Y-=1}`])
      break;
    case "W":
      X - 1 < 0 ? true : self.moveTrace.push([`${X-=1}` , `${Y}`])
  }
});

Currently, further move instructions into the wall's directions are completely ignored. If user would like to have data on where did Hoover hit the wall or how frequent the Hoover is stopped to improve the instruction-making process, calculations can be placed on the true condition of the ternary operator.

  1. The original plan is to use .includes to count the patches of dirt being cleaned by Hoover's movement. Only at the later stage of development, I realised arrays cannot be directly compared on js. Adjustment on the counting method was made:
self.dirtCoord.map( i => {
  if (JSON.stringify(self.moveTrace).includes(JSON.stringify(i))){
    self.dirtCleaned++
  } 
})

I altered the switch to store the movement coordinate this.moveTrace as string in arrays; then used JSON.stringify to allow .includes to be implemented. Potential problem on comparing array using JSON.stringify have not yet been found, but I simply (somehow) feel struggled that coordinates are being stored as string.

About


Languages

Language:JavaScript 100.0%