有一些问题,为什么不对我的字符串进行切片[closed]

问题描述 投票:0回答:1
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的切片部分并返回整个内容?     
javascript loops for-loop tdd slice
1个回答
0
投票
j的值在您的代码中介于17到19之间。var cardNumvar prefix的13个字符+ 3个字符。因此,您产生的cardNum变量的长度基本上为16。slice函数的语法为str_name.slice(startPointPosition, endPointPosition);在endPointPosition以上的代码中,即var j的值始终大于cardNum的总长度。如果将j的值更改为小于16,则可以看到更改。
© www.soinside.com 2019 - 2024. All rights reserved.