Created
December 2, 2023 18:52
-
-
Save sparrowDom/11d33d041a85935bbdcb33d781acca64 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++) { | |
// for each item feed 1 letter into the accumulator | |
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 case 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