在理解为什么不切割我的字符串时遇到一些问题
Having some problems understanding why its not slicing my string
describe('China UnionPay', function() {
let expect = chai.expect;
for (var prefix = 624; prefix <= 626; prefix++) {
for (let j = 17; j <= 19; j++) {
let cardNum = `${prefix}7891123456789`;
(function(prefix) {
it(`it has a prefix of ${prefix} and a length of ${j}`, function() {
// console.log(`${cardNum.slice(0,j)}`)
console.log('typeof cardNum', typeof cardNum, ' ', 'length of string =>', j, 'card is not the length of j?', cardNum.slice(0, j))
expect(detectNetwork(cardNum.slice(0, j))).to.equal('China UnionPay');
})
})(prefix)
}
}
})
我希望此代码执行的操作是获取 cardNum
并从 0
切片到 j
当前所在的长度。我已经在前面添加了前缀,但不确定为什么它不返回 cardNum
的一部分并返回整个内容?
代码中 j
的值介于 17 和 19 之间。而字符串的长度即
var cardNum
是13个字符+var prefix
的3个字符。所以你得到的 cardNum
变量的长度基本上是 16。
slice 函数的语法是 str_name.slice(startPointPosition, endPointPosition);
在 endPointPosition 上面的代码中,即 var j 的值总是大于 cardNum
的总长度。
如果将 j
的值更改为小于 16,您可以看到更改。
describe('China UnionPay', function() {
let expect = chai.expect;
for (var prefix = 624; prefix <= 626; prefix++) {
for (let j = 17; j <= 19; j++) {
let cardNum = `${prefix}7891123456789`;
(function(prefix) {
it(`it has a prefix of ${prefix} and a length of ${j}`, function() {
// console.log(`${cardNum.slice(0,j)}`)
console.log('typeof cardNum', typeof cardNum, ' ', 'length of string =>', j, 'card is not the length of j?', cardNum.slice(0, j))
expect(detectNetwork(cardNum.slice(0, j))).to.equal('China UnionPay');
})
})(prefix)
}
}
})
我希望此代码执行的操作是获取 cardNum
并从 0
切片到 j
当前所在的长度。我已经在前面添加了前缀,但不确定为什么它不返回 cardNum
的一部分并返回整个内容?
代码中 j
的值介于 17 和 19 之间。而字符串的长度即
var cardNum
是13个字符+var prefix
的3个字符。所以你得到的 cardNum
变量的长度基本上是 16。
slice 函数的语法是 str_name.slice(startPointPosition, endPointPosition);
在 endPointPosition 上面的代码中,即 var j 的值总是大于 cardNum
的总长度。
如果将 j
的值更改为小于 16,您可以看到更改。