aboutsummaryrefslogtreecommitdiff
path: root/src/routes/+page.svelte
blob: 093347fd24f396a7a6c815ba0252e79a8607d792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<script lang="ts">
    import { DateTime } from "luxon";

    const getCurrentYear = () => DateTime.now().year;
    const getRelative = (year: number) =>
        DateTime.local(year).toRelative({ locale: "en" })!;

    let year = $state(getCurrentYear());
    let sqrt = $derived(year ** 0.5);
    let isPerfectSquare = $derived(Number.isInteger(sqrt));
    let lastSqrt = $derived(isPerfectSquare ? sqrt - 1 : Math.floor(sqrt));
    let nextSqrt = $derived(isPerfectSquare ? sqrt + 1 : Math.ceil(sqrt));
    let last = $derived(lastSqrt ** 2);
    let next = $derived(nextSqrt ** 2);
    let nextRelative: string = $state("");

    const update = () => {
        year = getCurrentYear();
        nextRelative = getRelative(next);
    };
    update();
    setInterval(update, 500);
</script>

<div id="container">
    <h1>Is the year a perfect square?</h1>
    {#if isPerfectSquare}
        <div id="answer">Yes.</div>
        <strong>{sqrt}</strong><sup>2</sup> = {year}.
    {:else}
        <div id="answer">No.</div>
    {/if}
    The last was
    <ruby>{last}<rp> (</rp><rt>{lastSqrt}<sup>2</sup></rt><rp>)</rp></ruby>, and
    the next is
    <ruby>{next}<rp> (</rp><rt>{nextSqrt}<sup>2</sup></rt><rp>)</rp></ruby>, {nextRelative}!
</div>

<style>
    #container {
        text-align: center;
        font-size: 2rem;
    }
    h1 {
        font-size: inherit;
        margin: 0;
        font-weight: normal;
    }
    #answer {
        font-size: 5em;
        margin: 0;
        font-weight: bold;
        font-family: sans-serif;
    }
</style>