Skip to content

Instantly share code, notes, and snippets.

@wrimik
Last active June 30, 2017 03:17
Show Gist options
  • Save wrimik/4503d67498e114230c5e40f10ad5c377 to your computer and use it in GitHub Desktop.
Save wrimik/4503d67498e114230c5e40f10ad5c377 to your computer and use it in GitHub Desktop.

Flatten Array

This is a simple exercise project for building and testing a recursive function. I added more comments than I probably should have.

This project is written in javascript, and uses Jasmine for unit testing

Since I have your attention, I wanted to address the reason my public github is so sparse. Most of my projects have been on a privately hosted installation of github. Here are some screenshots of my activity outside this profile (in case that's the kind of thing you look at).

I'm more than happy to discuss and demo any of my recent projects.

-Mike

/**
* accepts a nested array of integers,
* returns a flat array of integers.
* does not check for null, floats, or random objects
*
* var input = [1, 2, null, {}, [3, 4, [5]], 6, 7];
* console.log(flattenArray([]));
*
* @param a
* @returns {Array}
*/
function flattenArray(a) {
var out = []; //define our output
for (var i in a) {//iterate over the given array
var type = typeof(a[i]);
if (type == 'number') {//if it's a number
out.push(a[i]);//we add it to the output
} else if(type == 'object'){//otherwise we iterate over it again, recursively.
out.push.apply(out, flattenArray(a[i]));//and add the results back in to our output
} // and if it's not an object or a number, we don't care about it
// if we wanted to throw errors for strings we could do so here
}
return out; // then return it
}
/**
* tests js/flattenArray.js
*/
describe('flattenArray', function () {
var result;
beforeEach(function () {
result = [1, 2, 3, 4, 5, 6, 7];
});
it('handles empty arrays', function () {
var input = [];
expect(flattenArray(input)).toEqual([]);
});
it('returns empty array when passed non arrays', function () {
var input = 'test';
expect(flattenArray(input)).toEqual([]);
});
it('flattens nested arrays of integers', function () {
var input = [1, 2, [3, 4, [5]], 6, 7];
expect(flattenArray(input)).toEqual(result);
});
it('ignores unexpected strings', function () {
var input = [1, 2, 'unexpected string', [3, 4, [5]], 6, 7];
expect(flattenArray(input)).toEqual(result);
});
it('ignores unexpected null', function () {
var input = [1, 2, null, [3, 4, [5]], 6, 7];
expect(flattenArray(input)).toEqual(result);
});
it('ignores unexpected objects', function () {
var input = [1, 2, {}, [3, 4, [5]], 6, 7];
expect(flattenArray(input)).toEqual(result);
});
it('correctly parses objects with integers', function () {
var input = [1, 2, {'a': 3, 'b': 4}, 5, 6, 7];
expect(flattenArray(input)).toEqual(result);
});
it('correctly parses nested objects with integers', function () {
var input = [1, 2, {'a': 3, 'b': {'c': 4}}, 5, 6, 7];
expect(flattenArray(input)).toEqual(result);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment