Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 1.57 KB

File metadata and controls

61 lines (44 loc) · 1.57 KB

Challenge #3: 😏 The naughty elf

In Santa's workshop, a mischievous elf has been playing in the gift manufacturing line, adding or removing an unplanned step.

You have the original sequence of steps in the manufacturing process and the modified sequence that may include an extra step or be missing a step.

Your task is to write a function that identifies and returns the first extra step that has been added or removed in the manufacturing line. If there is no difference between the sequences, return an empty string.

const original = "abcd";
const modified = "abcde";

findNaughtyStep(original, modified); // 'e'

const original = "stepfor";
const modified = "stepor";

findNaughtyStep(original, modified); // 'f'

const original = "abcde";
const modified = "abcde";

findNaughtyStep(original, modified); // ''

Warning

To take into account:

  • There will always be one step of difference or none.

  • The modification can occur anywhere in the sequence.

  • The original sequence can be empty.

Solution

JavaScript

370 points

function findNaughtyStep(original, modified) {
  const [shorterText, longerText] = [original, modified].sort(
    (a, b) => a.length - b.length
  );
  return [...longerText].find((x, i) => shorterText[i] != x) ?? "";
}

TypeScript

370 points

function findNaughtyStep(original: string, modified: string): string {
  const [shorterText, longerText] = [original, modified].sort(
    (a, b) => a.length - b.length
  );
  return [...longerText].find((x, i) => shorterText[i] != x) ?? "";
}