Are you overconfident? Take the test!
---
format: typebulb/v1
name: Are you overconfident?
---
**code.tsx**
```tsx
import React, { useMemo, useState } from "react";
import { createRoot } from "react-dom/client";
interface Question {
question: string;
answer: number;
unit: string;
}
interface InsightData {
questions: Question[];
}
const INITIAL_QUESTIONS: Question[] = tb.insight<InsightData>()?.questions ?? [];
type GameState = "start" | "playing" | "finished";
function App() {
const [state, setState] = useState<GameState>("start");
const [currentIndex, setCurrentIndex] = useState(0);
const [inputs, setInputs] = useState({ low: "", high: "" });
const [results, setResults] = useState<{ low: number; high: number; correct: boolean }[]>([]);
const [questions, setQuestions] = useState<Question[]>(INITIAL_QUESTIONS);
const [isInferring, setIsInferring] = useState(false);
const [inferError, setInferError] = useState<string | null>(null);
const handleStart = () => {
setState("playing");
setCurrentIndex(0);
setResults([]);
setInputs({ low: "", high: "" });
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const low = parseFloat(inputs.low);
const high = parseFloat(inputs.high);
const actual = questions[currentIndex].answer;
const isCorrect = actual >= low && actual <= high;
const newResults = [...results, { low, high, correct: isCorrect }];
setResults(newResults);
if (currentIndex < questions.length - 1) {
setCurrentIndex(currentIndex + 1);
setInputs({ low: "", high: "" });
} else {
setState("finished");
}
};
const correctCount = results.filter((r) => r.correct).length;
const targetCount = Math.round(questions.length * 0.9);
const calibrationLevel = () => {
if (correctCount === targetCount) return "Perfectly Calibrated!";
return correctCount > targetCount
? "Underconfident (Too many correct)."
: "Overconfident (Too many incorrect).";
};
const progressLabel = useMemo(
() => `Question ${currentIndex + 1} of ${questions.length}`,
[currentIndex, questions.length]
);
const runInference = async () => {
setIsInferring(true);
setInferError(null);
try {
const insight = await tb.infer<InsightData>();
if (!insight?.questions?.length) return;
setQuestions(insight.questions);
setState("start");
setCurrentIndex(0);
setResults([]);
setInputs({ low: "", high: "" });
} catch (err) {
setInferError("Inference failed. Please try again.");
} finally {
setIsInferring(false);
}
};
if (state === "start") {
return (
<div className="container">
<button className="inference-trigger" onClick={runInference} disabled={isInferring}>
{isInferring ? "Generating..." : "Create new questions for topic..."}
</button>
{inferError && <p className="error">{inferError}</p>}
<h1>Are You Overconfident?</h1>
<p>Provide a range for each such that you are 90% certain the answer is within it.</p>
<p className="goal-text"><b>Goal: Get {targetCount} / {questions.length} correct</b></p>
<button onClick={handleStart}>Start Quiz</button>
</div>
);
}
if (state === "playing") {
const q = questions[currentIndex];
return (
<div className="container">
<button className="inference-trigger" onClick={runInference} disabled={isInferring}>
{isInferring ? "Generating..." : "Create new questions for topic..."}
</button>
<div className="progress">{progressLabel}</div>
<h2>{q.question}</h2>
<form onSubmit={handleSubmit} className="input-group">
<div className="field">
<label>Lower Bound ({q.unit})</label>
<input
type="number"
value={inputs.low}
onChange={(e) => setInputs({ ...inputs, low: e.target.value })}
required
step="any"
autoFocus
/>
</div>
<div className="field">
<label>Upper Bound ({q.unit})</label>
<input
type="number"
value={inputs.high}
onChange={(e) => setInputs({ ...inputs, high: e.target.value })}
required
step="any"
/>
</div>
<button type="submit" disabled={parseFloat(inputs.low) > parseFloat(inputs.high)}>
Submit Range
</button>
</form>
</div>
);
}
return (
<div className="container">
<button className="inference-trigger" onClick={runInference} disabled={isInferring}>
{isInferring ? "Generating..." : "Create new questions for topic..."}
</button>
<h1>Results</h1>
<div className="score-summary">
<div className="score-big">{correctCount} / {questions.length}</div>
<p className="calibration-text">{calibrationLevel()}</p>
</div>
<div className="results-list">
{questions.map((q, i) => (
<div key={i} className={`result-item ${results[i].correct ? "correct" : "incorrect"}`}>
<div className="result-q">{q.question}</div>
<div className="result-details">
<span>Range: {results[i].low} - {results[i].high}</span>
<span className="actual">Actual: <strong>{q.answer}</strong></span>
</div>
</div>
))}
</div>
<button onClick={handleStart} className="restart-btn">Try Again</button>
</div>
);
}
const container = document.getElementById("root");
const root = createRoot(container!);
root.render(<App />);
```
**styles.css**
```css
:root {
--bg: #ffffff;
--text: #1a1a1a;
--primary: #6366f1;
--primary-hover: #4f46e5;
--card-bg: #f8fafc;
--border: #e2e8f0;
--success: #10b981;
--danger: #ef4444;
--muted: #64748b;
--font-main: 'Lexend', system-ui, -apple-system, sans-serif;
}
@import url('https://fonts.googleapis.com/css2?family=Lexend:wght@300;400;500;600;700;800&display=swap');
html[data-theme="dark"] {
--bg: #161616;
--text: #f1f5f9;
--primary: #818cf8;
--primary-hover: #a5b4fc;
--card-bg: #202020;
--border: #2d2d2d;
--muted: #94a3b8;
color-scheme: dark;
}
body {
margin: 0;
font-family: var(--font-main);
background-color: var(--bg);
color: var(--text);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 2rem;
box-sizing: border-box;
}
.container {
max-width: 600px;
width: 100%;
text-align: center;
}
h1 { font-size: 2rem; margin-bottom: 1rem; }
p { line-height: 1.5; color: var(--text); opacity: 0.9; }
button {
background-color: var(--primary);
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 0.5rem;
font-size: 1rem;
cursor: pointer;
transition: background 0.2s;
font-weight: 600;
}
.inference-trigger {
margin-bottom: 2rem;
background: transparent;
color: var(--text);
border: 1px solid var(--border);
padding: 0.5rem 1rem;
border-radius: 999px;
font-size: 0.9rem;
font-weight: 400;
}
.inference-trigger:hover { background: var(--card-bg); border-color: var(--primary); }
button:hover { background-color: var(--primary-hover); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.input-group {
background: var(--card-bg);
padding: 2rem;
border-radius: 1rem;
border: 1px solid var(--border);
display: flex;
flex-direction: column;
gap: 1.5rem;
margin-top: 2rem;
}
.field {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
}
label { font-weight: 500; font-size: 0.9rem; }
input {
width: 100%;
box-sizing: border-box;
padding: 0.75rem;
border-radius: 0.4rem;
border: 1px solid var(--border);
background: var(--bg);
color: var(--text);
font-size: 1.1rem;
font-family: var(--font-main);
}
.progress {
font-size: 1.1rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--primary);
margin-bottom: 0.5rem;
display: block;
}
.goal-text {
text-transform: uppercase;
color: var(--primary);
letter-spacing: 0.1em;
font-weight: 700;
margin-top: 1.5rem;
}
.score-summary {
margin: 2rem 0;
padding: 2rem;
background: var(--card-bg);
border-radius: 1rem;
}
.score-big {
font-size: 3rem;
font-weight: 800;
color: var(--primary);
}
.calibration-text {
font-weight: 600;
margin-top: 1rem;
}
.results-list {
text-align: left;
display: flex;
flex-direction: column;
gap: 0.75rem;
margin-bottom: 2rem;
}
.result-item {
padding: 1rem;
border-radius: 0.5rem;
border-left: 4px solid var(--border);
background: var(--card-bg);
}
.result-item.correct { border-left-color: var(--success); }
.result-item.incorrect { border-left-color: var(--danger); }
.result-q { font-weight: 600; margin-bottom: 0.25rem; }
.result-details {
font-size: 0.9rem;
display: flex;
justify-content: space-between;
opacity: 0.8;
}
.actual { color: var(--primary); }
.error { color: var(--danger); font-size: 0.8rem; }
.restart-btn { width: 100%; }
.insight-preview {
margin-top: 1.5rem;
text-align: left;
background: var(--card-bg);
border: 1px solid var(--border);
border-radius: 0.75rem;
padding: 1rem;
}
.insight-preview h3 {
margin-top: 0;
}
.insight-preview ul {
margin: 0.5rem 0 0 1.25rem;
padding: 0;
color: var(--muted);
}
```
**index.html**
```html
<div id="root"></div>
```
**data.txt**
```txt
General Knowledge
```
**infer.md**
```md
General Knowledge
```
**insight.json**
```json
{
"questions": [
{ "question": "In what year did the Titanic sink?", "answer": 1912, "unit": "Year" },
{ "question": "What is the height of Mount Everest in meters?", "answer": 8848, "unit": "Meters" },
{ "question": "How many keys are on a standard piano?", "answer": 88, "unit": "Keys" },
{ "question": "In what year did the first Moon landing occur?", "answer": 1969, "unit": "Year" },
{ "question": "What is the diameter of the Earth in kilometers?", "answer": 12742, "unit": "km" },
{ "question": "How many countries are there in Africa?", "answer": 54, "unit": "Countries" },
{ "question": "What is the speed of light in a vacuum (km/s)?", "answer": 299792, "unit": "km/s" },
{ "question": "In what year was the first iPhone released?", "answer": 2007, "unit": "Year" },
{ "question": "What is the approximate population of Tokyo (metropolitan) in millions?", "answer": 37, "unit": "Million" },
{ "question": "What is the average distance from Earth to the Moon in kilometers?", "answer": 384400, "unit": "km" }
]
}
```
**config.json**
```json
{
"dependencies": {
"react": "^19.2.3",
"react-dom": "^19.2.3"
},
"description": "Are you overconfident? Take the test!",
"inference": {
"title": "Overconfidence Test Generator",
"dataTitle": "Topic",
"submitTitle": "Generate Test"
}
}
```