Giter VIP home page Giter VIP logo

fib-code-test's Introduction

Fibonacci Code Test

Node.js code test that deals with awaiting user input, timers and large fibonacci numbers.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

This readme also outlines my though process and reasoning around software architectural decisions.

Prerequisites

Node.js > v10.4.0

Usage

Installation

npm install
tsc:build

Run

npm start

Run Tests

npm test

Thought Process & Comments

BigInt & Node.js version

The requirement to check the first 1000 terms of the Fibonacci sequence meant standard javascript integers would break from about the 79th term onwards due to the MAX_SAFE_INTEGER (9007199254740991).

BigInt to the rescue! BigInt is supported natively in Node.js from v10.4.0 onwards.

Checking if number entered is Fibonacci

I opted for memoization to improve efficiency and storing it in array as a lookup rather than computing on the fly each time the user inputs a number.

The initial time taken to generate the array outweighs the repeated call upon user input especially the longer the program is run.

Displaying frequency and eventBus

I decided to handle this by creating a generic timer class which takes in an eventName that it'll emit an event on during each cycle. I also created a generic eventBus class which handles the emitting and registering of event handlers which can be used throughtout any app.

This seemed the best approach to decouple the timer and displaying of the frequency and allows for futher modularity/udpates.

Main CLI

The main application has been broken down into basically 3 states that (The last two are very similar but for ease of code readability I decided not to merge).

  1. Asking for Frequency
  2. Asking for First number
  3. Asking for next number

These are all contained in while loops which requires a valid input to be entered before progressing to the next state or running an action.

Question Handler

The question handler outputs all questions, validation errors and handles their responses. During any of these 3 stages it will always check if one of the action words is inputted resume, halt & quit before going to the validation stage of ensuring an integer is entered.

Storing Entered Number

For performance reasons I decided in storing the inputted numbers and their frequency in a key value pair rather than just inputting them directly into an array and upon displaying to the user calculate the frequency and then order the output.

Originally I was planning on using an object and when outputting the display sorting the properties (inputted number) by their values (frequency) in descending order as requested by the problem. The issue with this is object property order didn't appear to be guaranteed and is prone to errors. Hence the reason of using a Map type where property order is guaranteed.

License

MIT

fib-code-test's People

Contributors

lukemuller avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.