- Typescript
- TS Compiler
- ESLint + Prettier
- Clone the repository
- Run
yarn
to install necessary dependencies. - Run
yarn build
to compile your code. - Run
yarn start
to run your code and see the result on terminal.
(scroll down to see more information about every task)
- Create reduce function
- Create find function
- Create concat function
- Create pipe function
All the functions should not use regular Array.prototype.reduce(), Array.prototype.concat() or Array.prototype.find(), they should be realized using only for(), while() and etc ..
- It should have functionality to stop cycle using stop function
const arr = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const obj = reduce(
arr,
(stop) => (acc, current, index) => {
if (index === 3) {
stop();
}
return { ...acc, [index]: current };
},
{}
);
console.log(obj); // Result: { 0: 'spray', 1: 'limit', 2: 'elite' }
const arr = [
{ id: 0, name: 'spray' },
{ id: 1, name: 'limit' },
];
console.log(find(arr, (item) => item.id === 1)); // Result: { id: 1, name: 'limit' }
console.log(find(arr, (item) => item.id === 10)); // Result: undefined
- The number of arrays should be unlimited. It can be 2, 3 or even 10.
const arr1 = ['spray', 'limit', 'elite'];
const arr2 = ['exuberant', 'destruction'];
const arr3 = ['present'];
concat(arr1, arr2); // Result: ['spray', 'limit', 'elite', 'exuberant', 'destruction']
concat(arr1, arr2, arr3); // Result: ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']
- All functions such as reduce, find & concat should not expect first argument (array) in case when they're in a pipe, it should be passed automatically.
const arr1 = ['spray', 'limit', 'elite'];
const arr2 = ['exuberant', 'destruction'];
const arr3 = ['present'];
pipe(arr1, [
concat(arr2, arr3), // Result: ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']
reduce(
(stop) => (acc, current, index) => {
if (acc.length === 2) {
stop();
}
// get only even elements
if (index % 2 == 0) {
acc.push({ index, label: current });
}
return acc;
},
[]
), // Result: [{ index: 2, label: 'limit' }, { index: 4, label: 'exuberant' }]
find((item) => item.label.length > 5), // Result { index: 4, label: 'exuberant' }
]);
// Result { index: 4, label: 'exuberant' }