Git Product home page Git Product logo

Comments (9)

benjamindonnachie avatar benjamindonnachie commented on June 21, 2024

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.

benjamindonnachie avatar benjamindonnachie commented on June 21, 2024

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.

benjamindonnachie avatar benjamindonnachie commented on June 21, 2024

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.

benkeen avatar benkeen commented on June 21, 2024

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.

benkeen avatar benkeen commented on June 21, 2024

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.

benjamindonnachie avatar benjamindonnachie commented on June 21, 2024

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.

benjamindonnachie avatar benjamindonnachie commented on June 21, 2024

A few example panNums from the start of the function might do it.

from generatedata.

benkeen avatar benkeen commented on June 21, 2024

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.

benjamindonnachie avatar benjamindonnachie commented on June 21, 2024

Great news - thanks for the update.

from generatedata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.