---
title: "Calculator 🧮"
description: "Using code blocks to solve math problems"
input:
- name: maths_question
type: string
description: "The math question to solve"
---
Here's a maths question:
{% $frontmatter.input.maths_question %}
Write a JavaScript function that returns the result of this question. Do not log anything. Output just the code and a call to the function.
{% ai #generated_code model="openai/gpt-4o" /%}
{% $generated_code.result %}
`\`\`\js {% #eval %}
// We pass code that was generated as a string into \`eval\` which will execute it
// First we remove any backticks/wrappers around the code
const code = aimVariables.generated_code.result.replaceAll(/\`\`\`javascript/g, "").replaceAll(/\`\`\`js/g, "").replaceAll(/\`\`\`JavaScript/g, "").replaceAll(/\`\`\`JS/g, "").replaceAll(/\`\`\`/g, "");
console.log("vars", code);
return eval(code);
`\`\`\
The answer is: {% $eval.result %}