Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Array.prototype.join() #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions implementations/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
*
MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

The join() method concats every element in array
with a defined separator and returns a new string.

Example 1 - Without a separator as parameter:
['a', 'e', 'i', 'o', 'u'].join();
must return:
'a,e,i,o,u'

Example 2 - With a undefined as parameter:
['a', 'e', 'i', 'o', 'u'].join(undefined);
must return:
'a,e,i,o,u'

Example 3 - With a empty string as parameter
['a', 'e', 'i', 'o', 'u'].join('');
must return:
'aeiou'

Example 4 - With a string as parameter
['a', 'e', 'i', 'o', 'u'].join('-');
must return:
'a-e-i-o-u'

Example 5 - With a function as parameter
['a', 'e', 'i', 'o', 'u'].join(() => {});
must return:
'a() => {}e() => {}i() => {}o() => {}u'

Example 6 - With a object as parameter
['a', 'e', 'i', 'o', 'u'].join({});
must return:
'a[object Object]e[object Object]i[object Object]o[object Object]u'

Example 7 - With an array as parameter
['a', 'e', 'i', 'o', 'u'].join([1, 2, 3]);
must return:
'a1,2,3e1,2,3i1,2,3o1,2,3u'

Example 8 - With a boolean as parameter
['a', 'e', 'i', 'o', 'u'].join(true);
must return:
'atrueetrueitrueotrueu'
*/

Array.prototype.join = function join(separator) {
let returnValue = '';
let sep = separator;

if (Object.prototype.toString.call(sep) === '[object Undefined]') {
sep = ',';
}

for (let index = 0; index < this.length; index += 1) {
returnValue += this[index] + (index < this.length - 1 ? sep : '');
}

return returnValue;
};
53 changes: 53 additions & 0 deletions implementations/join.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require('./join');

describe('join', () => {
const data = ['a', 'e', 'i', 'o', 'u'];

describe('when doesn\'t pass parameters', () => {
test('return \'a,e,i,o,u\'', () => {
expect(data.join()).toBe('a,e,i,o,u');
});
});

describe('when pass \'undefined\' as parameter', () => {
test('return \'a,e,i,o,u\'', () => {
expect(data.join(undefined)).toBe('a,e,i,o,u');
});
});

describe('when pass \'\' as parameter', () => {
test('return \'aeiou\'', () => {
expect(data.join('')).toBe('aeiou');
});
});

describe('when pass a string as parameter', () => {
test('return \'a-e-i-o-u\'', () => {
expect(data.join('-')).toBe('a-e-i-o-u');
});
});

describe('when pass a function as parameter', () => {
test('return \'a() => {}e() => {}i() => {}o() => {}u\'', () => {
expect(data.join(() => {})).toBe('a() => {}e() => {}i() => {}o() => {}u');
});
});

describe('when pass a object as parameter', () => {
test('return \'a[object Object]e[object Object]i[object Object]o[object Object]u\'', () => {
expect(data.join({})).toBe('a[object Object]e[object Object]i[object Object]o[object Object]u');
});
});

describe('when pass an array as parameter', () => {
test('return \'a1,2,3e1,2,3i1,2,3o1,2,3u\'', () => {
expect(data.join([1, 2, 3])).toBe('a1,2,3e1,2,3i1,2,3o1,2,3u');
});
});

describe('when pass a boolean as parameter', () => {
test('return \'atrueetrueitrueotrueu\'', () => {
expect(data.join(true)).toBe('atrueetrueitrueotrueu');
});
});
});