round brackets validation

Validate string for propper brackets amount and order.
Empty string should return `true`.

``````const validate = str => {
let acc = 0;

for (let i = 0; i < str.length; i += 1) {
acc = str[i] === '(' ? acc + 1 : acc - 1;
if (acc < 0) return false;
}

return acc === 0;
};``````
v1zvs. v1z

Find all dividors of a number

Algorithms
Code
Diff
• ``````const getDividors = n => {

let result = [];

for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }

return result; // output array won't be sorted

}``````
•  1 - const getDividors = (n, result = []) => { 1 + const getDividors = n => { 2 + 3 + let result = []; 2 2 3 - for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++) 5 + for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) 4 4 if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) } 5 5 6 6 return result; // output array won't be sorted 7 7 8 8 }

Flatten an array of arrays

Algorithms
Code
Diff
• ``````const flatten = arr =>
arr.reduce((acc, item) =>  acc.concat(Array.isArray(item) ? flatten(item) : item), []);``````
•  1 1 const flatten = arr => 2 - arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : [item]), []); 2 + arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);

Flatten an array of arrays

Algorithms
``````const flatten = arr =>
arr.reduce((acc, item) => Array.isArray(item) ? [...acc, ...flatten(item)] : [...acc, item], []);``````

Find all dividors of a number

Algorithms

The fastest way to find all dividors of a number.

``````const getDividors = (n, result = []) => {

for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)
if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }

return result; // output array won't be sorted

}``````