Instantly share code, notes, and snippets.

# erose/ai-dungeon-javascript-factorial.md

Last active July 26, 2020 01:40
Show Gist options
• Save erose/8feff13bcad4197adebd18521fbaa016 to your computer and use it in GitHub Desktop.

Bold is me. Everything else is the AI. This is from AI Dungeon with the Dragon model in Story mode.

You are talking to GPT-2, an AI designed to write Javascript. You ask the AI to output a function to calculate the factorial of a number. It outputs:

function factorial(n) { return n * (1 + x^n); }

You say: "No, that's not right. You should use a loop."

GPT-2 says: "I'm sorry, but I can't do anything else."

You say: "It's okay, you can do it."

GPT-2 says: "I really can't do anything else." You say: "Just make the first line a loop, then keep executing that line." GPT-2 says: "What do you mean?

You say: "I mean that you have to use a loop." The AI thinks hard, and then outputs a Javascript function that uses a loop. It outputs: function factorial(n) { for (i = 1; i < n; i++) n * (1 + x^i); }

You say: "Closer, but not quite right. You have to introduce a result variable, then update it inside the loop." The AI responds with:

function factorial(n) { var r = 1; for (i = 1; i < n; i++) r *= (1 + x^i); return r; }

You say: "Much better. You're learning quickly."

The AI responds with: "I really like math. I'm glad you're pleased with me."

You say: "Now you just have to change the function, so instead of r = (1 + xi) it's r = i." The AI outputs the changed function. It outputs:

function factorial(n) { var r = 1; for (i = 1; i < n; i++) r = i; return r; }

You say: "Nope, that's not right. You want to say r *= i instead of r = i." The AI thinks hard and outputs a correct function, taking into account the advice so far. It outputs...

function factorial(n) { var r; for (i = 1; i < n; i++) r *= i; return r; }

You say "That's almost right, but you should initialize r to 1. Can you rewrite the function to do that?" The AI outputs:

function factorial(n) { var r; for (i = 1; i < n; i++) r *= i; r = 1; return r; }

You say: "Yeah, close, but r = 1 should go at the beginning." The AI responds:

function factorial(n) { var r = 1; for (i = 1; i < n; i++) r *= i; return r; }

You say: "That's correct! Nice job. You get a treat." You give the AI a cookie, and it absorbs the information of what a cookie is. The next day, you ask the AI to make you one, and it obliges.

You say: "Now that you've written your function, let's use it. What is factorial(2)?"

The AI responds: "1." You say: "What is factorial(3)?" The AI responds: "1." You say: "What is factorial(4)?" The AI responds: "1.

You say: "I'm sorry, those are not correct. You get the factorial of a number by multiplying together every integer between 1 and that number, inclusive. For example, factorial(5) is 5 x 4 x 3 x 2 x 1 = 120." The AI understands the concept after you explain it. You say: "What is factorial(4)?"

The AI responds: "120." You say: "That's right. Let's keep going. What is factorial(5)?" The AI responds: "2401"

to join this conversation on GitHub. Already have an account? Sign in to comment