Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function netherRealms(input) {
- let array = input.split(/,\s*/);
- let healthPattern = /([^0-9+\-*\/\.+])/g ;
- let demagePatern = /[-+]?\d+(\.\d+)?/g;
- let divideOrMultiply = /(?<multiply>[*])|(?<divide>[\/])/g;
- let namePattern = /[\s[^\,]/g
- let resultHealth = 0
- let resultDemage = 0
- let object = {};
- for (let string of array) {
- let isTrue = namePattern.test(string)
- if (string.length > 0 && isTrue === false){
- object[string]={}
- let health = [...string.matchAll(healthPattern)]
- let demage = [...string.matchAll(demagePatern)]
- let divideOrMultiplies = [...string.matchAll(divideOrMultiply)]
- for (let i = 0; i < health.length; i++) {
- let text = health[i][0]
- let textAsCodeAscii = text.charCodeAt()
- resultHealth +=textAsCodeAscii
- }
- object[string]['health'] = resultHealth
- resultHealth = 0;
- for (let x = 0; x < demage.length; x++) {
- let number = Number(demage[x][0])
- resultDemage +=number
- }
- for (let el of divideOrMultiplies) {
- if (el.groups.multiply === '*'){
- resultDemage*=2
- }else if (el.groups.divide === '/'){
- resultDemage/=2
- }
- }
- object[string]['damage'] = resultDemage;
- resultDemage = 0;
- }
- }
- let arrNewMap = Object.entries(object).sort((a,b)=> (a[0]).localeCompare(b[0]))
- for (let [name, arrays] of arrNewMap) {
- console.log(`${name} - ${arrays['health']} health, ${arrays['damage'].toFixed(2)} damage`)
- }
- }
- netherRealms('bla,Aza44454zel//,Azazel');
Add Comment
Please, Sign In to add comment