Public
Edited
Dec 3, 2023
Insert cell
Insert cell
Insert cell
d3.sum(
input
.split("\n")
.map(
(d) =>
[...d].find((c) => c.match(/[0-9]/)) +
[...d].reverse().find((c) => c.match(/[0-9]/))
)
)
Insert cell
Insert cell
d3.sum(
input.split("\n"),
numm
)
Insert cell
numm = (t) => value(digit(t, fwd(t))) + value(digit(t, bw(t)))
Insert cell
numm("eightwo") // for @visnup
Insert cell
fwd = ((string) => d3.range(string.length).find((i) => digit(string, i)))
Insert cell
bw = (string) =>
d3
.range(string.length)
.reverse()
.find((i) => digit(string, i))
Insert cell
digits = [
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
]
Insert cell
digit = (text, i) =>
digits.find((d) => text.slice(i).startsWith(d))
Insert cell
function value(d) {
return String(digits.indexOf(d) % 10);
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more