testjs

Benchmark created on


Setup

function createChristmasTree(ornaments, height) {
  let ornamentIdx = 0;
  let tree = '';
  const spaces = ' '.repeat(height);
  for (let level = 1; level <= height; level++) {
    tree += spaces.slice(0, height - level);
    for (let charPosition = 0; charPosition < level * 2 - 1; charPosition++)
      if (charPosition % 2 === 0) {
        tree += ornaments[ornamentIdx];
        ornamentIdx = (ornamentIdx + 1) % ornaments.length;
      } else {
        tree += ' ';
      }
    tree += '\n';
  }
  return tree + spaces.slice(0, height - 1) + '|\n';
}

Test runner

Ready to run.

Testing in
TestOps/sec
case1
createChristmasTree("123", 215)
ready
case2
createChristmasTree("xo", 164)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.