launcher — arcade
launcher — JS Trivia

ARCADE // Glass

JS Trivia

Questions from my JavaScript book

From my JavaScript book

Loading the cartridge…

B back to the arcade

Code of this demo: JS Trivia
"use client";import { useRef, useState } from "react";import { questions, rankFor } from "@/data/trivia";import { play } from "@/lib/sound";import { useConsoleInput, useStep } from "../input";import { HowTo, useHowTo } from "../HowTo"; type Phase = "ask" | "feedback" | "end"; const T = {  en: { score: "Score", streak: "Streak", pick: "↑↓ choose · Enter / click: confirm", next: "Enter: next", right: "Right!", wrong: "Not quite.", again: "Enter: try again", rank: "Rank", of: "of" },  es: { score: "Puntos", streak: "Racha", pick: "↑↓ elegir · Enter / clic: confirmar", next: "Enter: siguiente", right: "¡Correcto!", wrong: "Incorrecto.", again: "Enter: otra vez", rank: "Rango", of: "de" },}; /** JavaScript trivia drawn from the book's topics. arrows choose, Enter or a click confirms. */export function BookDemo({ locale }: { locale: "en" | "es" }) {  const t = T[locale];  const step = useStep();  const how = useHowTo("book");  const [n, setN] = useState(0);  const [sel, setSel] = useState(0);  const [phase, setPhase] = useState<Phase>("ask");  const [score, setScore] = useState(0);  const [streak, setStreak] = useState(0);  const [best, setBest] = useState(0);  const [ok, setOk] = useState(false);  const live = useRef({ n, sel, phase, streak, score });  live.current = { n, sel, phase, streak, score };   const answer = (choice: number) => {    const c = live.current;    if (c.phase !== "ask") return;    step("answer");    const right = choice === questions[c.n].answer;    setSel(choice);    setOk(right);    setPhase("feedback");    if (right) {      play("confirm");      setScore(c.score + 1);      setStreak(c.streak + 1);      setBest(Math.max(best, c.streak + 1));    } else {      play("back");      setStreak(0);    }  };   useConsoleInput((a) => {    if (how.openRef.current) { if (a === "a" || a === "b") how.hide(); return; }    const c = live.current;    if (c.phase === "ask") {      if (a === "up" || a === "down") {        play("blip");        setSel((s) => (s + (a === "down" ? 1 : 2)) % 3);      } else if (a === "a") {        answer(c.sel);      }    } else if (c.phase === "feedback" && a === "a") {      if (c.n + 1 >= questions.length) {        step("next");        play("epic");        setPhase("end");      } else {        step("render");        play("flip");        setN(c.n + 1);        setSel(0);        setPhase("ask");      }    } else if (c.phase === "end" && a === "a") {      play("boot");      setN(0); setSel(0); setScore(0); setStreak(0); setBest(0); setPhase("ask");    }  });   if (phase === "end") {    return (      <div className="dm dm-book">        <p className="bk-title">{t.rank}</p>        <p className="bk-rank">{rankFor(score, questions.length, locale)}</p>        <p className="dm-note" aria-live="polite">{score} {t.of} {questions.length} · {t.streak} {best}</p>        <p className="dm-hint">{t.again}</p>      </div>    );  }   const q = questions[n];  return (    <div className="dm dm-book">      <div className="bk-bar">        <span>{n + 1}/{questions.length}</span>        <span>{t.score} {score}</span>        <span className={streak > 1 ? "hot" : undefined}>{t.streak} {streak}</span>      </div>      <pre className="bk-code"><code>{q.code}</code></pre>      <p className="bk-q">{q.q[locale]}</p>      <ul className="bk-opts">        {q.options.map((o, i) => {          const state = phase === "feedback" ? (i === q.answer ? "right" : i === sel ? "wrong" : "") : "";          return <li key={i} className={`${i === sel ? "sel" : ""} ${state}`.trim()}><button type="button" onClick={(e) => { e.currentTarget.blur(); answer(i); }} disabled={phase !== "ask"}><span aria-hidden="true">{i === sel ? "▸" : " "}</span> {o[locale]}</button></li>;        })}      </ul>      <p className="dm-note bk-fb" aria-live="polite">{phase === "feedback" ? <><b>{ok ? t.right : t.wrong}</b> {q.why[locale]}</> : ""}</p>      <p className="dm-hint">{phase === "ask" ? t.pick : t.next}</p>      <HowTo id="book" locale={locale} how={how} />    </div>  );}

Simplified recreation made for this site, not the project source.

  • Go to Map
  • Go to About
  • Go to Projects
  • Go to Experience
  • Go to Stack
  • Go to Arcade
  • Go to Contact
  • Go to Direct view
  • Open chapter I · TCG Store
  • Open chapter II · AmplixMe FS-0009
  • Open chapter III · IKAI Shell
  • Open chapter IV · JavaScript para Subir de Nivel
  • Open chapter V · omarchy-zen
  • Open chapter VI · obsidian-backend-template
  • Open chapter VII · Skorify (community contributions)
  • Open chapter VIII · Fitness_Tracker
  • Open chapter IX · linux-win-setup
  • Open chapter X · weather-app-react
  • Open chapter XI · studio-ghilbli-api
  • Theme: Chipre
  • Theme: Páramo
  • Theme: Tinto
  • Theme: Cafetal
  • Theme: Niebla
  • Theme: Nevado
  • Theme: Cable
  • Theme: Catedral
  • Download CV
  • Copy email
  • Cambiar a Español

↑↓ browse · Enter run · Esc close