Skip to content

Instantly share code, notes, and snippets.

@EliaMelfior
Last active December 15, 2019 20:51
Show Gist options
  • Save EliaMelfior/61288b615c5ef035f0b4e83aa7fefe0c to your computer and use it in GitHub Desktop.
Save EliaMelfior/61288b615c5ef035f0b4e83aa7fefe0c to your computer and use it in GitHub Desktop.
Flattening an Array With Tests
class MyArray {
constructor() {
this.flattenedArray = [];
}
flattenArray(array) {
let me = this;
array.forEach(function(element) {
if (!Array.isArray(element)) {
me.flattenedArray.push(element);
} else {
me.flattenArray(element);
}
});
}
}
// specs code
describe("My Array Tests", function() {
let unflattenedArray = new MyArray();
let arrayToChange = [[1,2,[3]],4];
it("Should initialize the class", function() {
expect(unflattenedArray.flattenedArray).toEqual([]);
});
it("Should flatten [[1,2,[3]],4]", function() {
unflattenedArray.flattenArray([[1,2,[3]],4]);
expect(unflattenedArray.flattenedArray).toEqual([1,2,3,4]);
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
@EliaMelfior
Copy link
Author

It can bee tested here:
https://jsfiddle.net/1by0jokq/3/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment