NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


/*
Write a function which reverses the passed string
*/

const reverseString = (str) => {
if(str==''){
return str;
}
return str.split('').reverse().join('');
}

/*
Write a function which removes the passed delimiter from the string.
e.g. 'a-b-c'.removeDelimiter('-') should return 'abc'
*/

String.prototype.removeDelimiter = function(delimiter) {
return this.split(delimiter).join('');
};

/*
Write a function which return the multiplication of three numbers.
The function can be called in any of the following forms:

multiply(2, 3, 4) => 24
multiply(2, 3)(4) => 24
multiply(2)(3, 4) => 24
multiply(2)(3)(4) => 24
*/

let curry = (fn) => {
return function curryFn(...args1) {
if (args1.length >= fn.length) {
return fn(...args1);
} else {
return (...args2) => curryFn(...args1, ...args2);
}
}
}

function product(x,y,z) {
return x*y*z;
}

const multiply = curry(product);

/*
write a function to check if the two object passed are equal or not.
the objects can be nested.
*/

const areEqualObjs = (obj1, obj2) => {
return JSON.stringify(obj1) === JSON.stringify(obj2)
}

/*
write a function to faltten the array
*/

Array.prototype.flatten = function() {
return this.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? toFlatten.flatten() : toFlatten);
}, []);
};


//Tests

describe('reverseString', () => {
it('reverses the passed string', () => {
expect(reverseString('')).toBe('');
expect(reverseString('abcde')).toBe('edcba');
});
});

describe('removeDelimiter', () => {
it('removes the passed delimiter from the string', () => {
expect(reverseString('')).toBe('');
expect("Equal-Experts".removeDelimiter('-')).toEqual('EqualExperts');
});
});

describe('multiply', () => {
it('curries the multiply for three arguments', () => {
expect(multiply(2, 3, 4)).toBe(24);
expect(multiply(2, 3)(4)).toBe(24);
expect(multiply(2)(3, 4)).toBe(24);
expect(multiply(2)(3)(4)).toBe(24);
});
});

describe('areEqualObjs', () => {
it('compares the objects', () => {
expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 2
})).toBeTruthy();

expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1,
b: 3
})).toBeFalsy();

expect(areEqualObjs({
a: 1,
b: 2
}, {
a: 1
})).toBeFalsy();

expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 1
}
})).toBeTruthy();

expect(areEqualObjs({
a: {
b: 1
}
}, {
a: {
b: 2
}
})).toBeFalsy();

expect(areEqualObjs({
a: {
b: 1,
c: {
d: 2
}
}
}, {
a: {
b: 1,
c: {
d: 3
}
}
})).toBeFalsy();

expect(areEqualObjs({
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
}, {
a: {
b: 1,
c: {
d: {
e: 2
}
}
}
})).toBeTruthy();
})
});

describe('flattenArray',() => {
it('flattens the passed array', () => {
expect([].flatten()).toEqual([]);
expect([1, 2].flatten()).toEqual([1, 2]);
expect([1, [2]].flatten()).toEqual([1, 2]);
expect([1, [2, 3]].flatten()).toEqual([1, 2, 3]);
expect([[1], [2], [3], [4], [5]].flatten()).toEqual([1, 2, 3, 4, 5]);
expect([1, 2, [3, [4, 5, 6]], [7, 8]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
expect([1, [2, [3, [4, [5, [6, [7, [8]]]]]]]].flatten()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
});






     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.