Last active
December 15, 2019 20:51
-
-
Save EliaMelfior/61288b615c5ef035f0b4e83aa7fefe0c to your computer and use it in GitHub Desktop.
Flattening an Array With Tests
This file contains 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
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(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can bee tested here:
https://jsfiddle.net/1by0jokq/3/