Skip to content

weaponsforge/jsalgos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsalgos

A collection of solutions to classic problems using javascript. A boilterplate for basic javascript nodejs environment and methods testing with mocha.

Dependecies

  1. NodeJS
    • version 10.16.3 was used for this project
  2. Windows 10 OS 64-bit
  3. VS Code
    • for debugging (breakpoints)

Tip

Follow JavaScript programming best practices and patterns from Clean Code JavaScript while solving the CodeWars problems.

Content

  1. Scripts
  2. Installation
  3. Usage

Scripts

/scripts

Main methods and functions are saved inside the /scripts directory. These are proposed solutions for codewars problems.

Usage:

const methods = require('./scripts')
methods.formatDuration(362)
methods.mixedFraction('97/25')
methods.parseInt('one hundred and five')
...
  1. format-duration.js - [link]
    Accepts time input in seconds and returns a human-readable conversion format.

  2. mixed-fraction.js - [link]
    Converts a fraction to its lowest terms mixed fraction form.

  3. parse-string-int.js - [link]
    Converts an English string of a number to its numerical value.

  4. sum-by-factors.js - [link]
    Generate a list of all prime factors from array elements matched with the sum of array element(s) to which it is a prime factor of.

  5. smallest.js - [link]
    Get the smallest combination from the input number while moving only (1) digit.

  6. top-three-words.js - [link]
    Retrieves the top (3) most used words from a sentence.

  7. dependency-injection.js - [link]
    Testing for dependency injection where the kata's idea is to be able to execute the functions passed as arguments in any specified order.

  8. array-rotate.js - [link]
    Rotate array A K times; that is, each element of A will be shifted to the right K times.

  9. binary-gap.js - [link]
    Find the largest binary gap - longest streak of consecutive zeroes between 1's in the binary conversion of a Number

  10. odd-occurrences.js - [link]

    A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

    • Goal is to write a function that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
  11. mergesort.js
    Classic javascript implementation of the merge sort method

/scripts/utils

Contains various reusable utility scripts. Methods and functions are saved inside the /scripts/utils directory.

Usage:

const { factors, Binary, findRepeating } = require('./scripts/utils')
factors(25)
Binary.toNumber('101')
findRepeating(145614561852)
...
  1. factors.js
    Returns an array of a number's factors not including itself.

  2. binary.js toNumber: Converts a str
    ing of binary sequence into its number format. toBinary: Converts a Number into Binary format

  3. findRepeating.js (WIP)
    Finds the repeating sequence of a number in String format.

Installation

  1. Clone this repository. https://github.com/weaponsforge/jsalgos.git

  2. Install dependencies. npm install

Usage

A. Create New Scripts

  1. Create a new script for module.exports inside /scripts/*.js following the format <METHOD-NAME>.js
  2. Require the newly-created script inside /scripts/index.js
  3. Refer to the Testing and Debugging sections to test your new script.

B. Testing

  1. To run current tests:
    npm test

  2. To create or edit new tests:

    • Edit existing test values from /test/test-values/*.js that corresponds as input to target functions from /scripts/*.js For example, mixedfraction-test.js are valid test values for format-duration.js or
    • Create new test values following the format <METHOD_NAME>-test.js inside /test/test-values/*.js. Add it as modules.exports to /test/test-values/index.js
  3. Use the new or updated tests:

    • Open /test/test-values/test.js. Edit this script as needed for testing.

    • Write mocha tests for the target function available in the method object (i.e., method.formatDuration(), etc.) using your new test values available in the tests object.

        tests.mixedFraction.forEach((item, index) => {
          it(`Test Passed: Value === ${item.answer}`, () => {
            assert.equal(method.formatDuration(item.input), item.answer)
          })
        })
      
    • Run the tests: npm test

C. Debugging

Debug using VS Code.

  1. Open jsalgos/index.js

  2. Call the function to evaluate, for example format-duration.js

     const script = require('./scripts')
     let a = script.formatDuration(120)
     console.log(a)
    
  3. Set breakpoints in /scripts/format-duration.js using VS Code.

  4. Start debugging: Press F5

20191101

About

A collection of solutions to classic problems using javascript.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors