aboutsummaryrefslogtreecommitdiff
path: root/src/routes/+page.svelte
blob: 3bdd11df1226fd01791b05e505d53d433fc8bede (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
56
57
58
59
60
61
62
<script lang="ts">
    import { DateTime } from "luxon";
    import RubySquareYear from "./RubySquareYear.svelte";
    import Square from "./Square.svelte";

    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 next = $derived(nextSqrt ** 2);
    let nextRelative = $state("");

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

<div id="mainDisplay">
    <h1>Is the year a perfect square?</h1>
    {#if isPerfectSquare}
        <div id="answer">Yes.</div>
        <strong>{sqrt}</strong><Square /> = {year}.
    {:else}
        <div id="answer">No.</div>
    {/if}
    The last was
    <RubySquareYear sqrt={lastSqrt} /> and the next is
    <RubySquareYear sqrt={nextSqrt} />, {nextRelative}!
</div>

<div id="sourceLinkContainer">
    <a href="https://github.com/arjunsatarkar/is-the-year-a-perfect-square">source code</a>
</div>

<style>
    #mainDisplay {
        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;
    }
    #sourceLinkContainer {
        text-align: right;
    }
</style>