Question: A javascript question on creating an array of unique values from all given arrays. Given the test, write the corresponding function: There's a union.js file

A javascript question on creating an array of unique values from all given arrays.

Given the test, write the corresponding function:

There's a union.js file that needed to be implemented the function (TO DO is optional):

A javascript question on creating an array of unique values from all

The function has to pass the following tests in file union.test.js:

given arrays. Given the test, write the corresponding function: There's a union.js

Please help me with this problem

Use the following test text so it's easier for you to copy and paste:

import union from './union'; describe('when union arrays', () => { it('should return union of 2 arrays', () => { const afterUnion = union([2], [1, 2]); expect(afterUnion).toEqual([2, 1]); }); it('should find union from a list of arrays', () => { const afterUnion = union([2], [1, 2], [2, 3]); expect(afterUnion).toEqual([2, 1, 3]); }); it('should not flatten nested arrays', () => { const afterUnion = union([1, 3, 2], [1, [5]], [2, [4]]); expect(afterUnion).toEqual([1, 3, 2, [5], [4]]); }); it('should order union by their first encounter', () => { const afterUnion = union([10, 20], [1, 30, 10], [0, 40]); expect(afterUnion).toEqual([10, 20, 1, 30, 0, 40]); }); });

* This function creates an array of unique values, in order, from all given * arrays. The result array should contains all the values of the input arrays. * Please note that this function should correctly handle 'undefined', null" * and 'NaN'. Two 'undefined values should be considered as equal, two * "NaN' values should be cofnsidered as equal though 'NaN !== Nan' and so does * *null'. * @param {... any} arrays The input arrays. */ export default function union (...arrays) { // TODO: Please implement the function // } // TODO // You can add additional method if you want // import union from './union'; describe( name: 'when union arrays', fn: () => { it( name: 'should return union of 2 arrays', fn: () => { const afterUnion = union( arrays: [2], [1, 2]); expect(afterUnion).toEqual( expected: [2, 1]); }); it( name: 'should find union from a list of arrays', fn: () => { const afterUnion = union( arrays: [2], [1, 2], [2, 3]); expect(afterUnion).toEqual( expected: [2, 1, 3]); }); it( name: 'should not flatten nested arrays', fn: () => { const afterUnion = union( arrays: [1, 3, 2], [1, [5]], [2, [4]]); expect(afterUnion).toEqual( expected: [1, 3, 2, [5], [4]]); }); it( name: 'should order union by their first encounter', fn: () => { const afterUnion = union( arrays: (10, 20], [1, 30, 10], [0, 40]); expect(afterUnion).toEqual( expected: (10, 20, 1, 30, 0, 40]); }); })

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!