Credit card checker

Hello everyone, I need help with the credit card checker project, I can’t implement luhn’s algorithm, someone can help me. Thank you

Hello! This is an article of Luhn’s algorithm. I can’t help anymore without seeing your code.

5 * 2  =>  10  =>  1 + 0  =>  1  <= 10 - 9
6 * 2  =>  12  =>  1 + 2  =>  3  <= 12 - 9
7 * 2  =>  14  =>  1 + 4  =>  5  <= 14 - 9
8 * 2  =>  16  =>  1 + 6  =>  7  <= 16 - 9
9 * 2  =>  18  =>  1 + 8  =>  9  <= 18 - 9

That’s the math of it, either way.

Now to plan which digits in the two lists below get the math done on them.

[5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]

[3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]

Once the math is done we just add up the values and divide by 10. If the remainder is zero, it’s a valid cc number.


The algorithm starts from the right side, second element in, moving left. We see a variety of approaches that attack it from that point and work right to left. Seems like a lot of work to me.

Why not just measure the length and decide which element to begin on, and work from left to right? Parity solves this. The starting element is the length modulo 2.

Proof of concept

>>> def luhn(s):
	def f(a):
		return sum([*map(int, [*str(a)])])
	for n in range((len(s) % 2), len(s), 2):
		s[n] = f(s[n] * 2)
	return sum(s) % 10 == 0

>>> u = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]
>>> v = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]
>>> luhn(u)
True
>>> luhn(v)
True
>>> p = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]
>>> q = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]
>>> luhn(p)
False
>>> luhn(q)
False
>>> 
1 Like

Proof of concept, this time in the correct language…

const luhn = t => {
  const g = (a, b) => +a + +b
  const f = x => [...(x.toString())].reduce(g)
  const s = t.slice()
  for (let n = s.length % 2; n < s.length; n += 2) {
    s[n] = f(s[n] * 2)
  }		
  return s.reduce(g) % 10 == 0
}

This code did not come about by accident. And it is not a first draft of this problem, given the conditions. Anybody who skips those first steps will never get to this point on their own. Don’t use this code unless you can write it yourself.

1 Like

https://repl.it/@mtf/ccCheckerjs

I tried to see the solution, to find out if I could figure it out and do it again on my own, even after all this, I can’t figure it out.

Hello!
please can I get the link to this project “Credit card checker”?