Ad

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;
};
Algorithms
Logic
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
      
    }
    • const getDividors = (n, result = []) => {
    • const getDividors = n => {
    • let result = [];
    • for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)
    • 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
    • }
Algorithms
Logic
Code
Diff
  • const flatten = arr =>
      arr.reduce((acc, item) =>  acc.concat(Array.isArray(item) ? flatten(item) : item), []);
    • const flatten = arr =>
    • arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : [item]), []);
    • arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);
Algorithms
Logic
const flatten = arr =>
  arr.reduce((acc, item) => Array.isArray(item) ? [...acc, ...flatten(item)] : [...acc, item], []);
Algorithms
Logic

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
  
}