Sha256: be04abbf3f2380ea7f5587dbafbca35771feff36235510dc0528b155093b9bf6

Contents?: true

Size: 701 Bytes

Versions: 165

Compression:

Stored size: 701 Bytes

Contents

const lastArgIsOptions = (args) => {
  const last = args[args.length - 1];
  return typeof last === 'object';
};

const conclusion = (firstArg, qualifier = '') => `And all for the want of a ${qualifier}${firstArg}.`;

const proverb = (...args) => {
  let options = {};
  if (lastArgIsOptions(args)) {
    options = args.pop();
  }

  const allExceptLastArg = args.slice(0, -1);
  const chainOfEvents = allExceptLastArg.map((arg, index) => `For want of a ${arg} the ${args[index + 1]} was lost.`);

  const qualifier = options.qualifier ? `${options.qualifier} ` : options.qualifier;
  chainOfEvents.push(conclusion(args[0], qualifier));

  return chainOfEvents.join('\n');
};

export default proverb;

Version data entries

165 entries across 165 versions & 1 rubygems

Version Path
trackler-2.2.1.18 tracks/ecmascript/exercises/proverb/example.js
trackler-2.2.1.17 tracks/ecmascript/exercises/proverb/example.js
trackler-2.2.1.16 tracks/ecmascript/exercises/proverb/example.js
trackler-2.2.1.15 tracks/ecmascript/exercises/proverb/example.js
trackler-2.2.1.14 tracks/ecmascript/exercises/proverb/example.js