A collection of solutions to classic problems using javascript. A boilterplate for basic javascript nodejs environment and methods testing with mocha.
- NodeJS
- version 10.16.3 was used for this project
- Windows 10 OS 64-bit
- VS Code
- for debugging (breakpoints)
Tip
Follow JavaScript programming best practices and patterns from Clean Code JavaScript while solving the CodeWars problems.
- Scripts
- main scripts - /scripts
- utility scripts - /scripts/utils
- Installation
- Usage
Main methods and functions are saved inside the /scripts directory. These are proposed solutions for codewars problems.
const methods = require('./scripts')
methods.formatDuration(362)
methods.mixedFraction('97/25')
methods.parseInt('one hundred and five')
...
-
format-duration.js - [link]
Accepts time input in seconds and returns a human-readable conversion format. -
mixed-fraction.js - [link]
Converts a fraction to its lowest terms mixed fraction form. -
parse-string-int.js - [link]
Converts an English string of a number to its numerical value. -
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. -
smallest.js - [link]
Get the smallest combination from the input number while moving only (1) digit. -
top-three-words.js - [link]
Retrieves the top (3) most used words from a sentence. -
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. -
array-rotate.js - [link]
Rotate array A K times; that is, each element of A will be shifted to the right K times. -
binary-gap.js - [link]
Find the largest binary gap - longest streak of consecutive zeroes between 1's in the binary conversion of a Number -
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.
-
mergesort.js
Classic javascript implementation of the merge sort method
Contains various reusable utility scripts. Methods and functions are saved inside the /scripts/utils directory.
const { factors, Binary, findRepeating } = require('./scripts/utils')
factors(25)
Binary.toNumber('101')
findRepeating(145614561852)
...
-
factors.js
Returns an array of a number's factors not including itself. -
binary.js
toNumber: Converts a str
ing of binary sequence into its number format.toBinary: Converts a Number into Binary format -
findRepeating.js (WIP)
Finds the repeating sequence of a number inStringformat.
-
Clone this repository.
https://github.com/weaponsforge/jsalgos.git -
Install dependencies.
npm install
- Create a new script for module.exports inside /scripts/*.js following the format
<METHOD-NAME>.js - Require the newly-created script inside /scripts/index.js
- Refer to the Testing and Debugging sections to test your new script.
-
To run current tests:
npm test -
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.jsare valid test values forformat-duration.jsor - Create new test values following the format
<METHOD_NAME>-test.jsinside /test/test-values/*.js. Add it as modules.exports to /test/test-values/index.js
- Edit existing test values from /test/test-values/*.js that corresponds as input to target functions from /scripts/*.js For example,
-
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
methodobject (i.e.,method.formatDuration(), etc.) using your new test values available in thetestsobject.tests.mixedFraction.forEach((item, index) => { it(`Test Passed: Value === ${item.answer}`, () => { assert.equal(method.formatDuration(item.input), item.answer) }) }) -
Run the tests:
npm test
-
Debug using VS Code.
-
Open
jsalgos/index.js -
Call the function to evaluate, for example
format-duration.jsconst script = require('./scripts') let a = script.formatDuration(120) console.log(a) -
Set breakpoints in
/scripts/format-duration.jsusing VS Code. -
Start debugging: Press F5
20191101