32
$\begingroup$

I was digging though this grim looking code base from like thousands of years ago. I came across this class function, but the class's name was obfuscated. What should this class be named?

class ? { async collectPayment(customer) { let attempts = 3; while (attempts > 0) { const response = this.query(customer); attempts--; if (response === this.constructor.name) throw new Error('Promise broken.'); if (attempts > 0) await customer.spin(86400); } this.addChild(customer.removeChild(customer.firstChild)); } } 

EDIT: Fixed typo in last line of code.

$\endgroup$
2
  • $\begingroup$ Is this JavaScript code? $\endgroup$ Commented Oct 11 at 0:11
  • 1
    $\begingroup$ Yes, it ran when I gave it some mocks. It was a piece of JavaScript code I saw that inspired this puzzle, but the language isn't important. $\endgroup$ Commented Oct 12 at 2:57

1 Answer 1

33
$\begingroup$

The class should be called

Rumpelstiltskin

Because it

represents the imp from the eponymous fairy tale.

async collectPayment(customer)

In the story, Rumplestiltskin comes to collect the girl's first child.

let attempts = 3; while (attempts > 0) { const response = this.query(customer); attempts--;

She gets three days to guess his name.

if (response === this.constructor.name) throw new Error('Promise broken.');

If she correctly guesses his name, she doesn't have to give him her first born as promised.

if (attempts > 0) await customer.spin(86400);

She gets three days and there are 86400 seconds in a day.

this.addChild(customer.removeChild(child));

If she does not guess correctly (and throw an error to get out of the function) then she loses her child to Rumplestiltskin!

Easter egg

The class was found in a "grim looking code base". The fairy tale was collected and recorded by the Brothers Grimm.

$\endgroup$
3
  • $\begingroup$ Whole time reading this recalled me of my childhood… $\endgroup$ Commented Oct 10 at 4:46
  • 10
    $\begingroup$ Bug report: Awaiting only after querying causes an off-by-one error: Spec sheet says this code should run for three whole days, now it only takes two (-and-change). $\endgroup$ Commented Oct 10 at 13:54
  • 2
    $\begingroup$ Ah, of course! There needs to be a day's grace before the first query. Thank you for your insightful review! $\endgroup$ Commented Oct 12 at 22:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.