Public
Edited
Dec 2, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
answer1 = input
.split("\n")
.map((d) => d.replace(/\D+/g, "").split(""))
.map((d) => Number(d[0] + d[d.length - 1]))
.reduce((a, b) => a + b)
Insert cell
Insert cell
Insert cell
answer2[0]
Insert cell
answer2 = example2.split("\n")
Insert cell
answer2[1].match(/^(one|two|three|four|five|six|seven|eight|nine|\d)/)
Insert cell
parseInt("one")
Insert cell
"sadfasthreedfasdf".match(/^(one|two|three|four|five|six|seven|eight|nine|\d)/)
Insert cell
// stolen!
function wordToDigits(input) {
let result = "";
while (input.length) {
const match = input.match(
/^(one|two|three|four|five|six|seven|eight|nine|\d)/
);

if (match) {
const i = parseInt(match[0]);
if (isNaN(i)) {
// If the input starts with a word, find its corresponding value
result += digitStrings.find((d) => d.str == [match[0]])["num"];
} else {
// If the input starts with a digit, add it to the result
result += i;
}
} else {
result += input[0];
}
input = input.slice(1);
}
return result;
}
Insert cell
wordToDigits(input)
Insert cell
answer22 = wordToDigits(input)
.split("\n")
.map((d) => d.replace(/\D+/g, "").split(""))
.map((d) => Number(d[0] + d[d.length - 1]))
.reduce((a, b) => a + b)
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