简体   繁体   English

这个CompareAmounts函数到底在做什么? (分析)

[英]What exactly is this CompareAmounts function doing? (Analysis)

I am analyzing a long JS code but I'm new to JS. 我正在分析很长的JS代码,但是我是JS新手。 Despite my best efforts, I have only been able to analyze around 85% of the code. 尽管尽了最大的努力,但我只能分析大约85%的代码。 This function CompareAmounts is still eluding me. 这个功能CompareAmounts仍然让我难以理解。 This JS is supposed to run on a bank webpage, and I can't understand what this function is doing. 该JS应该在银行网页上运行,我不知道此功能在做什么。 Could anyone please point out the functionality to me? 有人可以向我指出功能吗?

function CompareAmounts(a, b) {
        var c = /^\-/gi;
        var d = "";
        var e = "";
        if (a.match(c)) {
            a = a.replace(c, "");
            a = a.replace(".", "");
            a = a.split(",");
            a[0] = (parseInt(a[0]) - parseInt(b)).toString();
            if (parseInt(a[0]) < 0) {
                a[0] = (a[0] * -1).toString();
            } else {
                d = "-";
                e = "";
            }
        } else {
            a = a.replace(".", "");
            a = a.split(",");
            a[0] = (parseInt(a[0]) + parseInt(b)).toString();
        }
        if (a[0].length > 3) {
            var f = a[0].substr(0, a[0].length - 3);
            var g = a[0].substr(a[0].length - 3, a[0].length);
            a[0] = f + "." + g;
        }
        a = d + a.toString() + e;
        return a;
    }

It's a fairly convoluted way of adding one number to another number - a + b . 这是将一个数字加到另一个数字a + b的一种相当复杂的方法。

It contains extra logic to parse a as a European-style number in string form (remove all the . s, take the part before the , and then reconstruct the result in European number format afterward (incorrectly if the a or the result is greater than 999999 ). 它包含额外的逻辑, a将a解析为字符串形式的欧式数字(删除所有. ,在之前加入部分,然后在之后以欧洲数字格式重建结果(如果a或结果大于999999 )。

It does not have the same logic in place for parsing b , so I would presume that b is being passed in as a number value rather than a string. 它没有相同的逻辑来解析b ,因此我假设b作为数字值而不是字符串传递。

 function CompareAmounts(a, b) { var c = /^\\-/gi; var d = ""; var e = ""; if (a.match(c)) { a = a.replace(c, ""); a = a.replace(".", ""); a = a.split(","); a[0] = (parseInt(a[0]) - parseInt(b)).toString(); if (parseInt(a[0]) < 0) { a[0] = (a[0] * -1).toString(); } else { d = "-"; e = ""; } } else { a = a.replace(".", ""); a = a.split(","); a[0] = (parseInt(a[0]) + parseInt(b)).toString(); } if (a[0].length > 3) { var f = a[0].substr(0, a[0].length - 3); var g = a[0].substr(a[0].length - 3, a[0].length); a[0] = f + "." + g; } a = d + a.toString() + e; return a; } snippet.log(CompareAmounts("1.234,55", 12)); snippet.log(CompareAmounts("-10", 5)); snippet.log(CompareAmounts("50.400,80", 100)); snippet.log(CompareAmounts("1.000.000,00", 1)); // incorrect result snippet.log(CompareAmounts("-50,80", 100)); // incorrect result 
 <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM