old | const items = {"torit": 30, "gorot": 100};
const fruitName = /(?<=\:).+?(?=\:)/g;
function handleErrors(args) {
const numOfArgs = args.length;
let errors = [], wrongWords = [];
for (let i = 0; i < numOfArgs; i++) {
if (args[i].indexOf("👉") < 0) {
const matchResult = args[i].match(fruitName);
if (matchResult && matchResult.length > 0) {
const itemName = matchResult[0];
if (!items[itemName])
wrongWords.push(`<${args[i]}>`);
}
}
}
if(wrongWords.length)
errors.push("Items not found: " + wrongWords.join(", "));
return errors.join("\n");
}
function inputDataProcess(args) {
const fruitName = /(?<=\:).+?(?=\:)/g;
let numOfArgs = args.length, posOfSeparator = 0, itemsBeforeSeparator = [], itemsAfterSeparator = [];
let trading = "", lf = "";
if(handleErrors(args).length)
throw new Error(handleErrors(args));
while(1) {
if(args[posOfSeparator].indexOf("👉") >= 0)
break;
else
posOfSeparator++;
}
let l = 0;
for(l; l < posOfSeparator; l++) {
itemsBeforeSeparator.push(args[l].match(fruitName)[0]);
trading += `<${args[l]}>` + " ";
};
l++
for(l; l < numOfArgs; l++) {
itemsAfterSeparator.push(args[l].match(fruitName)[0]);
lf += `<${args[l]}>` + " ";
}
return {
itemsBeforeSeparator,
itemsAfterSeparator,
tradeResult: trading,
lfResult: lf,
};
}
function profitCalculate(itemsBeforeSeparator, itemsAfterSeparator) {
let sumBefore = 0, sumAfter = 0, tradeStatus = "";
for(let i = 0; i < itemsBeforeSeparator.length; i++)
sumBefore += items[itemsBeforeSeparator[i]];
for(let i = 0; i < itemsAfterSeparator.length; i++)
sumAfter += items[itemsAfterSeparator[i]];
const profit = ((sumAfter - sumBefore) * 100) / sumAfter;
profit < 0 ? (tradeStatus = "невыгодная", embedColor = "#ED4245") : (tradeStatus = "выгодная", embedColor = "#57F287");
let biggest = sumBefore > sumAfter ? sumBefore : sumAfter;
let smaller = sumBefore < sumAfter ? sumBefore : sumAfter;
if(biggest - smaller <= 20)
tradeStatus = "равноценная", embedColor = "Blue";
return { sumBefore, sumAfter, profit, embedColor, tradeStatus };
}
function main(args) {
try {
const { tradeResult, lfResult, itemsBeforeSeparator, itemsAfterSeparator } = inputDataProcess(args);
const { sumBefore, sumAfter, profit, embedColor, tradeStatus } = profitCalculate(itemsBeforeSeparator, itemsAfterSeparator);
const formattedProfit = Math.abs(profit).toFixed(2);
let statusString;
if (tradeStatus === "выгодная")
statusString = `Вы получили ${formattedProfit}% из этой сделки`;
else if (tradeStatus === "невыгодная")
statusString = `Вы потеряли ${formattedProfit}% из этой сделки`;
else
statusString = `В этой сделки вы ничего не потеряли и не получили`;
console.log(statusString);
} catch(err) {
console.error(err.message);
}
}
main([
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
'👉',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335'
]);
| ready |
new | const items = {"torit": 30, "gorot": 100};
const itemNameRegex = /(?<=\:).+?(?=\:)/g;
function handleErrors(args) {
const numOfArgs = args.length;
let errors = [], wrongWords = [];
for(let i = 0; i < numOfArgs; i++) {
const currentArg = args[i];
const isInvalid = currentArg.indexOf("👉") >= 0 || !currentArg.match(itemNameRegex) || items[currentArg.match(itemNameRegex)];
if(isInvalid)
continue;
wrongWords.push(`<${args[i]}>`);
}
if(wrongWords.length)
errors.push("Items not found: " + wrongWords.join(", "));
return errors.join("\n");
}
function inputDataProcess(args) {
const itemNameRegex = /(?<=\:).+?(?=\:)/g;
let itemsBeforeSeparator = [], itemsAfterSeparator = [];
let trading = "", lf = "";
if(handleErrors(args).length)
throw new Error(handleErrors(args));
let isSeparator = false;
for(let i = 0; i < args.length; i++) {
if(args[i].indexOf("👉") >= 0) {
isSeparator = true;
continue;
}
if(!isSeparator) {
itemsBeforeSeparator.push(args[i].match(itemNameRegex)[0]);
trading += `<${args[i]}> `;
} else {
itemsAfterSeparator.push(args[i].match(itemNameRegex)[0]);
lf += `<${args[i]}> `;
}
}
return {
itemsBeforeSeparator,
itemsAfterSeparator,
trading,
lf,
};
}
const equalValueBoundary = 20;
function profitCalculate(itemsBeforeSeparator, itemsAfterSeparator) {
let sumBefore = 0, sumAfter = 0, tradeStatus = 0;
for (let i = 0; i < itemsBeforeSeparator.length || i < itemsAfterSeparator.length; i++) {
if (i < itemsBeforeSeparator.length) {
sumBefore += items[itemsBeforeSeparator[i]];
}
if (i < itemsAfterSeparator.length) {
sumAfter += items[itemsAfterSeparator[i]];
}
}
const profit = ((sumAfter - sumBefore) * 100) / sumAfter;
profit < 0 ? (tradeStatus = 0, embedColor = "Red") : (tradeStatus = 1, embedColor = "Green");
const biggest = sumBefore > sumAfter ? sumBefore : sumAfter;
const smaller = sumBefore < sumAfter ? sumBefore : sumAfter;
if(biggest - smaller <= equalValueBoundary)
tradeStatus = -1, embedColor = "Blue";
return { sumBefore, sumAfter, profit, embedColor, tradeStatus };
}
function main(args) {
try {
const { trading: tradeResult, lf: lfResult, itemsBeforeSeparator, itemsAfterSeparator } = inputDataProcess(args);
const { sumBefore, sumAfter, profit, embedColor, tradeStatus } = profitCalculate(itemsBeforeSeparator, itemsAfterSeparator);
const formattedProfit = Math.abs(profit).toFixed(2);
let statusString;
switch(tradeStatus) {
case -1: {
statusString = `**Это равноценная сделка** | В этой сделки вы ничего не потеряли и не получили`;
};
case 0: {
statusString = `**Это невыгодная сделка** | Вы потеряли ${formattedProfit}% из этой сделки`;
};
case 1: {
statusString = `**Это выгодная сделка** | Вы получили ${formattedProfit}% из этой сделки`;
}
}
console.log(statusString);
} catch(err) {
console.log(err);
}
}
main([
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
':torit:1068131428589645864',
'👉',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335',
':gorot:1068128925269643335'
]);
| ready |