Skip to content

Instantly share code, notes, and snippets.

@sparrowDom
Created December 2, 2023 18:54
Show Gist options
  • Save sparrowDom/13baf3986a5065ed1ec9a8c4ffcb4f4a to your computer and use it in GitHub Desktop.
Save sparrowDom/13baf3986a5065ed1ec9a8c4ffcb4f4a to your computer and use it in GitHub Desktop.
let re = /\D*/g
input
.map(item => {
// replace map
digiMap =
[
['one', '1'],
['two', '2'],
['three', '3'],
['four', '4'],
['five', '5'],
['six', '6'],
['seven', '7'],
['eight', '8'],
['nine', '9']
]
acc = "";
for (h = 0; h < item.length; h++) {
// feed 1 letter at a time to accumulator to match words in order of first occurrence
acc += item[h];
for (let j = 0; j < digiMap.length; j++) {
// replace any values in the accumulator - yay performance
acc = acc.replaceAll(digiMap[j][0], digiMap[j][1])
}
}
return acc;
})
// remove all letters
.map(i => i.replaceAll(re, ''))
// take first and the last digit and cast to Integer
.map(i => parseInt(i[0] + i[i.length - 1]))
// sum up the values
.reduce((i,a) => i + a, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment