Comments (9)
I think this should do it...
Line 40:
while (pos < numChars-1) {
change to:
while (pos < numChars) {
(Logic - string is reversed - still need to process last digit as that's the first digit in the PAN)
Line 48:
if (pos != (numChars - 2)) {
change to:
if (pos +1 < numChars) {
(Logic - skip evens if the PAN length is odd. As the string is reversed and pos jumps in twos, test whether would exceed bounds).
Then testing with www.typescriptlang.org it generates correct Luhn check digits for generated even (e.g. VISA) and odd (e.g. Amex) length PAN numbers.
from generatedata.
Created pull request #844
Needs testing in full environment rather than just the http://www.typescriptlang.org/ playground. I'll try to get to this over the coming days.
from generatedata.
Tested using a PAN randomly generated using Generate Code ("6304764553616362"):
` //let panNums = "630476455361636"; // check digit should be 9.
let panNums = "63047645536163"; // check digit should be 4.
const numChars = panNums.length;
// const reversedNums = utils.stringUtils.reverse(panNums);
const reversedNums = panNums.split("").reverse().join("");
// calculate sum
let sum = 0;
let pos = 0;
while (pos < numChars) {
const currentNum: number = +reversedNums[pos];
let odd = currentNum * 2;
if (odd > 9) {
odd -= 9;
}
sum += odd;
if (pos +1 < numChars) {
const currentNum: number = +reversedNums[pos+1];
sum += +reversedNums[pos+1];
}
pos += 2;
}
// calculate check digit
// const checkDigit = 10 - (sum %10);
const checkDigit = ((Math.floor(sum/10) + 1) * 10 - sum) % 10;
console.log("Input")
console.log(panNums);
panNums += checkDigit;
console.log("Output");
console.log(panNums);
};`
Output for odd length:
[LOG]: "Input"
[LOG]: "63047645536163"
[LOG]: "Output"
[LOG]: "630476455361634"
Sum = (6 + 6 + 0 + 8 + 7 + 3 + 4 + 1 + 5 + 6 + 6 + 2 + 6 + 6 + 4) = 70
70 mod 10 = 0, that means this number is valid.
https://goodcalculators.com/luhn-algorithm-calculator/
© 2015-2023 goodcalculators.com
Even length:
[LOG]: "Input"
[LOG]: "630476455361636"
[LOG]: "Output"
[LOG]: "6304764553616369"
Sum = (3 + 3 + 0 + 4 + 5 + 6 + 8 + 5 + 1 + 3 + 3 + 1 + 3 + 3 + 3 + 9) = 60
60 mod 10 = 0, that means this number is valid.
https://goodcalculators.com/luhn-algorithm-calculator/
© 2015-2023 goodcalculators.com
Works standalone; needs testing within full environment.
from generatedata.
Thanks @benjamindonnachie! Looks good, but I'll leave this open until tomorrow night when I can confirm it's working on my end.
Appreciate the fix.
from generatedata.
No luck, I'm afraid. I tried a few Visa numbers and all were invalid. All complained about the last digit, e.g.
4287475778422
4929217516178
4916373745428
from generatedata.
No luck, I'm afraid. I tried a few Visa numbers and all were invalid. All complained about the last digit, e.g.
Thanks Ben. That's frustrating, when I try on the typescript playground I get the right results:
[LOG]: "Input"
[LOG]: "428747577842"
[LOG]: "Output"
[LOG]: "4287475778428"
[LOG]: "Input"
[LOG]: "492921751617"
[LOG]: "Output"
[LOG]: "4929217516174"
[LOG]: "Input"
[LOG]: "491637374542"
[LOG]: "Output"
[LOG]: "4916373745424"
Can you confirm how generatePAN() is called? If you could add a console.log(panNums); just before // calculate sum ? Or is there any chance of access to your test environment? (I suspect not but it's worth asking!).
Kind regards,
Benjamin
from generatedata.
A few example panNums from the start of the function might do it.
from generatedata.
Never mind! It was on my end, I apologize. The older code had been cached. When I wiped out the old web worker files I get green across the board for the PAN numbers I was generating. Thanks @benjamindonnachie! I'll get this into the next release. I'll try to get it out this weekend.
from generatedata.
Great news - thanks for the update.
from generatedata.
Related Issues (20)
- When using SQL and update, the primary_key ( id ) value goes back to 1 after it hits 50 HOT 1
- The tool breaks when changing the Date Format HOT 4
- Remove old Safari check code
- Error trying to run yarn startApp on Ubuntu 22.04 HOT 1
- Update "expired" toast for more info
- Homepage style tweak
- Upgrade to node 16
- "Help" footer link doesn't work in standalone app
- Upgrade to react 18
- Upgrade grunt
- Upgrade to node 18
- Track1 Options styling
- Inserting multiple copies of codemirror langs
- View latest history version throws error HOT 1
- TS: alphanumeric types set to `string` but should be number / number + string
- Add Copy option for preview panel
- Accounts: search by email not functioning properly
- error Command "startApp" not found.
- Sessions not properly initialized after registering
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generatedata.