简体   繁体   English

我的 for 循环无法正常工作,但我不知道为什么

[英]My for loop is not working properly however I have no idea why

So basically I'm writing a script that takes in a credit card number and outputs the provider name.所以基本上我正在编写一个脚本,该脚本接受信用卡号并输出提供商名称。 (Also if im doing this in an overly complicated way advice is appreciated) (另外,如果我以过于复杂的方式执行此操作,我们将不胜感激)

But my initial for loop to iterate over each object is not working.但是我最初的 for 循环遍历每个 object 不起作用。 It will get to 3 (even though the length of the arr is 8) and then just stop...它会达到 3(即使 arr 的长度是 8)然后停止......

I am dumbfounded as I have re-read the code for an hour now, checked everything I could think of but am just hitting a wall.我已经傻眼了,因为我已经重新阅读了一个小时的代码,检查了我能想到的所有内容,但我只是碰壁了。 Any help would be much appreciated!任何帮助将非常感激!

The Code编码

var detectNetwork = function(cardNumber) {
var splitDigits = cardNumber.split('');
var length = splitDigits.length;
var first = splitDigits[0];
var second = splitDigits[1];
var firstTwo = splitDigits[0] + splitDigits[1];
var firstThree = splitDigits[0] + splitDigits[1] + splitDigits[2];
var firstFour = splitDigits[0] + splitDigits[1] + splitDigits[2] + splitDigits[3];
var firstSix = splitDigits[0] + splitDigits[1] + splitDigits[2] + splitDigits[3] + splitDigits[4] + splitDigits[5];

var checkProvider = [
    {
        name: 'American Express',
        length: [ 15 ],
        prefixLength: [ 2 ],
        prefixType: 'simple',
        prefix: [ 34, 37 ]
    },

    {
        name: 'Diner\'s Club',
        length: [ 14 ],
        prefixLength: [ 2 ],
        prefixType: 'simple',
        prefix: [ 38, 39 ]
    },

    {
        name: 'Visa',
        length: [ 13, 16, 19 ],
        prefixLength: [ 1 ],
        prefixType: 'simple',
        prefix: [ 4 ]
    },

    {
        name: 'MasterCard',
        length: [ 16 ],
        prefixLength: [ 2 ],
        prefixType: 'range',
        prefix: [ 51, 55 ]
    },

    {
        name: 'Discover',
        length: [ 16, 19 ],
        prefixLength: [ 2, 3, 4],
        prefixType: 'complexRange',
        prefix: [ 6011, [ 644, 649 ], 65 ]
    },

    {
        name: 'Maestro',
        length: [ 12, 13, 14, 15, 16, 17, 18, 19],
        prefixLength: [ 4 ],
        prefixType: 'simple',
        prefix: [ 5018, 5020, 5038, 6304 ]
    },

    {
        name: 'China UnionPay',
        length: [ 16, 17, 18, 19 ],
        prefixLength: [ 3, 4, 6 ],
        prefixType: 'complexRange',
        prefix: [ [ 622126, 622925 ], [ 624, 626 ], [ 6282, 6288 ] ]
    },

    {
        name: 'Switch',
        length: [ 16, 18, 19 ],
        prefixLength: [ 4, 6 ],
        prefixType: 'simple',
        prefix: [ 4903, 4905, 4911, 4936, 564182, 633110, 6333, 6759 ]
    }
];

for(var i = 0; i < checkProvider.length; i++) {
    var currentProvider = checkProvider[i];
    var name = currentProvider.name;
    var lengthOptions = currentProvider.length;
    var prefixLengthOptions = currentProvider.prefixLength;
    var prefixType = currentProvider.prefixType;
    var prefixOptions = currentProvider.prefix;

    for(var j = 0; j < lengthOptions.length; j++) {
        var currentLength = lengthOptions[j];
        if (currentLength === length) {
            //Use first
            for (var y = 0; y < prefixLengthOptions.length; y++) {
                var currentPrefixLength = prefixLengthOptions[y];
                if(currentPrefixLength === 1) {
                    if (first === '4') {
                        if (firstFour !== 4903 && firstFour !== 4905 && firstFour !== 4911 && firstFour !== 4936) {
                            return name;
                        }
                    }
                }
                if (currentPrefixLength === 2) {
                    if (prefixType === 'complexRange') {
                        console.log(name);
                    }
                    if (prefixType === 'simple') {
                        for (var i = 0; i < prefixOptions.length; i++) {
                            if (firstTwo === prefixOptions[i].toString()) {
                                return name;
                            }
                        }
                    }
                    if (prefixType === 'range') {
                        var min = prefixOptions[0];
                        var max = prefixOptions[1];
                        for (var i = min; i < max + 1; i++) {
                            if (firstTwo === i.toString()) {
                                return name;
                            }
                        }
                    }  
                }
            }
        }
    }
}
}

Your test detectNetwork('6011123456789303') logs Discover if you make the following small changes to your code:您的测试detectNetwork('6011123456789303')记录Discover您是否对代码进行了以下小的更改:

  1. Replace all var with const .将所有var替换为const
  2. Declare all loop index variables with let .let声明所有循环索引变量。 For example, for (let i = 0; .例如, for (let i = 0; .

The issue with your current code is that you have var i redeclared within the same context.您当前代码的问题是您在同一上下文中重新声明了var i According to documentation , these cases do not trigger an error, however the behavior will be confusing when the values are overwritten, which is your case. 根据文档,这些情况不会触发错误,但是当值被覆盖时,行为会令人困惑,这是您的情况。

The suggestion is to rename the other var i to something else, and that should solve your problem.建议是将另一个var i重命名为其他名称,这应该可以解决您的问题。

Another option is to use let i which will make the context more strict .另一种选择是使用let i 这将使上下文更加严格

So this code would work best (I made it so if always returns the name):所以这段代码效果最好(我这样做了,如果总是返回名称):

const detectNetwork = function(cardNumber) {
const splitDigits = cardNumber.split('');
const length = splitDigits.length;
const first = splitDigits[0];
const second = splitDigits[1];
const firstTwo = splitDigits[0] + splitDigits[1];
const firstThree = splitDigits[0] + splitDigits[1] + splitDigits[2];
const firstFour = splitDigits[0] + splitDigits[1] + splitDigits[2] + splitDigits[3];
const firstSix = splitDigits[0] + splitDigits[1] + splitDigits[2] + splitDigits[3] + splitDigits[4] + splitDigits[5];

const checkProvider = [
    {
        name: 'American Express',
        length: [ 15 ],
        prefixLength: [ 2 ],
        prefixType: 'simple',
        prefix: [ 34, 37 ]
    },

    {
        name: 'Diner\'s Club',
        length: [ 14 ],
        prefixLength: [ 2 ],
        prefixType: 'simple',
        prefix: [ 38, 39 ]
    },

    {
        name: 'Visa',
        length: [ 13, 16, 19 ],
        prefixLength: [ 1 ],
        prefixType: 'simple',
        prefix: [ 4 ]
    },

    {
        name: 'MasterCard',
        length: [ 16 ],
        prefixLength: [ 2 ],
        prefixType: 'range',
        prefix: [ 51, 55 ]
    },

    {
        name: 'Discover',
        length: [ 16, 19 ],
        prefixLength: [ 2, 3, 4],
        prefixType: 'complexRange',
        prefix: [ 6011, [ 644, 649 ], 65 ]
    },

    {
        name: 'Maestro',
        length: [ 12, 13, 14, 15, 16, 17, 18, 19],
        prefixLength: [ 4 ],
        prefixType: 'simple',
        prefix: [ 5018, 5020, 5038, 6304 ]
    },

    {
        name: 'China UnionPay',
        length: [ 16, 17, 18, 19 ],
        prefixLength: [ 3, 4, 6 ],
        prefixType: 'complexRange',
        prefix: [ [ 622126, 622925 ], [ 624, 626 ], [ 6282, 6288 ] ]
    },

    {
        name: 'Switch',
        length: [ 16, 18, 19 ],
        prefixLength: [ 4, 6 ],
        prefixType: 'simple',
        prefix: [ 4903, 4905, 4911, 4936, 564182, 633110, 6333, 6759 ]
    }
];

for(let i = 0; i < checkProvider.length; i++) {
    let currentProvider = checkProvider[i];
    let name = currentProvider.name;
    let lengthOptions = currentProvider.length;
    let prefixLengthOptions = currentProvider.prefixLength;
    let prefixType = currentProvider.prefixType;
    let prefixOptions = currentProvider.prefix;

    for(let j = 0; j < lengthOptions.length; j++) {
        let currentLength = lengthOptions[j];
        if (currentLength === length) {
            //Use first
            for (let y = 0; y < prefixLengthOptions.length; y++) {
                let currentPrefixLength = prefixLengthOptions[y];
                if(currentPrefixLength === 1) {
                    if (first === '4') {
                        if (firstFour !== 4903 && firstFour !== 4905 && firstFour !== 4911 && firstFour !== 4936) {
                            return name;
                        }
                    }
                }
                if (currentPrefixLength === 2) {
                    if (prefixType === 'complexRange') {
                        return(name);
                    }
                    if (prefixType === 'simple') {
                        for (let i = 0; i < prefixOptions.length; i++) {
                            if (firstTwo === prefixOptions[i].toString()) {
                                return name;
                            }
                        }
                    }
                    if (prefixType === 'range') {
                        let min = prefixOptions[0];
                        let max = prefixOptions[1];
                        for (let i = min; i < max + 1; i++) {
                            if (firstTwo === i.toString()) {
                                return name;
                            }
                        }
                    }  
                }
            }
        }
    }
}
}

var n = detectNetwork('6011123456789303');
console.log(n);  // Discover

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

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