blob: 3b2ef88bb407ab74fa6dd6713295c0dcd0d391b8 (
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
|
<script lang="ts">
import { DateTime } from "luxon";
const getCurrentYear = () => DateTime.now().year;
const getRelative = (year) =>
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 = $state(getRelative(next));
setInterval(() => {
year = getCurrentYear();
nextRelative = getRelative(next);
}, 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>
|