JSFiddle - React, Tailwind, and code Playground
by PiereDome
JavaScript
key = {
'and': '',
'-': ' ',
'eleven': 11,
'twelve': 12,
'thirteen': 13,
'fourteen': 14,
'fifteen': 15,
'sixteen': 16,
'seventeen': 17,
'eighteen': 18,
'nineteen': 19,
'twenty': '2*',
'thirty': '3*',
'forty': '4*',
'fifty': '5*',
'sixty': '6*',
'seventy': '7*',
'eighty': '8*',
'ninety': '9*',
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
'hundred': '*00',
'thousand': '*,000',
'million': '*,000,000',
'billion': '*,000,000,000'
};
function clearEmpties(array) {
array = array.filter(function(e) {
if (e !== '') {
return e;
}
});
return array;
}
function parseEnglishValues(input) {
var total = tempTotal = 0;
values = [];
input = input.toLowerCase();
for (x in key) {
var test = new RegExp(x, 'g');
input = input.replace(test, key[x]);
}
input = input.split(' ');
input = clearEmpties(input);
for (i = input.length - 1; i > 0; i--) {
first = input[i - 1];
second = input[i];
len = first.length - second.length;
if (second.substr(0, 1) === '*') {
input[i - 1] = first + second.substr(1);
input[i] = '';
}
if (first.substr(-1) === '*') {
input[i - 1] = first.substr(0, first.length - 1) + second;
input[i] = '';
}
}
input = clearEmpties(input);
for (i = 0; i < input.length - 1; i++) {
first = input[i];
second = input[i + 1];
len = first.length - second.length;
if (first.length < second.length) {
count1 = first.match(/[0]/g).length;
count2 = second.match(/[1-9]/g).length;
count = count1<count2?count1:count2;
input[i+1] = first.substr(0,first.length-count)+second;
} else {
input[i + 1] =...