Last active
May 31, 2019 12:50
-
-
Save thardy/3a98b2360b6a85734ae359bea6104467 to your computer and use it in GitHub Desktop.
Software Solutions Architect - Remote Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
class FlattenService { | |
constructor() {} | |
flattenArray(arrayToBeFlattened, flatArray) { | |
for (let item of arrayToBeFlattened) { | |
if (Array.isArray(item)) { | |
this.flattenArray(item, flatArray); | |
} | |
else { | |
flatArray.push(item); | |
} | |
} | |
} | |
} | |
module.exports = FlattenService; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FlattenService = require('./flattenService'); | |
const chai = require('chai'); | |
const expect = chai.expect; | |
describe ('flattenArray Works', function () { | |
let flattenService = {}; | |
before (() => { | |
flattenService = new FlattenService(); | |
}); | |
after (() => { | |
}); | |
it ('can flatten an Array of Arrays of numbers', () => { | |
const myArray = [[1,2,[3]],4]; | |
let flatArray = []; | |
flattenService.flattenArray(myArray, flatArray); | |
expect(flatArray).to.deep.equal([1,2,3,4]); | |
}); | |
// just tinkering - the requirements didn't specifically ask for distinct, but it's easy to add by | |
// changing the flatArray.push(item); to if (!flatArray.find((x) => x === item)) { flatArray.push(item); } | |
// it ('can flatten an Array of Arrays of numbers into distinct array of numbers', () => { | |
// const myArray = [[1,2,[3,1]],4,2]; | |
// let flatArray = []; | |
// | |
// flattenService.flattenArray(myArray, flatArray); | |
// | |
// expect(flatArray).to.deep.equal([1,2,3,4]); | |
// }); | |
it ('can flatten an Array of Arrays of strings', () => { | |
const myArray = [['red','blue',['green']],'yellow']; | |
let flatArray = []; | |
flattenService.flattenArray(myArray, flatArray); | |
expect(flatArray).to.deep.equal(['red','blue','green','yellow']); | |
}); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "javascripttesting", | |
"version": "1.0.0", | |
"description": "", | |
"main": "src/flattenService.js", | |
"scripts": { | |
"test": "./node_modules/.bin/mocha src/**/*.spec.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": {}, | |
"devDependencies": { | |
"chai": "^4.2.0", | |
"mocha": "^6.1.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment