Williammer
6/18/2018 - 11:05 AM

interview-question-practices.js - of mine

const curry = (fn) => {
  const len = fn.length;
  let time = 1;
  let func = fn;

  const inner = (item) => {
    if (time < len) {
      func = func.bind(null, item);
      time++;
      return inner;
    }

    return func(item);
  }

  return (item) => inner(item);
}


const fn = (a, b, c) => a + b + c;

console.log("---: ", curry(fn)(1)(2)(3));


// // ----- next
Number.prototype.add = function(number) {
  return this + number;
};
Number.prototype.reduce = function(number) {
  return this - number;
};

const result = (10).add(10).reduce(2).add(10)
console.log('===== result:', result);


// ---- next
function parse(obj, pathStr) {
  const path = pathStr.split(/\.|\[/);
  let result = path.length && obj;

  while(path.length) {
    const curPath = path.shift();
    const intPath = parseInt(curPath, 10);
    const prop = !isNaN(intPath) ? intPath : curPath;
    result = result[prop];
  }

  return result || "undefined";
}

const object = {
 b: { c: 4 }, d: [{ e: 5 }, { e: 6 }]
};
console.log(parse(object, "b.c") == 4);
console.log(parse(object, "d[0].e") == 5);
console.log(parse(object, "d.0.e") == 5);
console.log(parse(object, "d[1].e") == 6);
console.log(parse(object, "d.1.e") == 6);
console.log(parse(object, "f") == "undefined");