Created
December 2, 2023 18:54
-
-
Save sparrowDom/13baf3986a5065ed1ec9a8c4ffcb4f4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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