Home My space log
Language EN (English)

Card number: the last digit

JavaScript Advanced astronaut ages teen+ From CS Unplugged

The problem

Bank card numbers end in a check digit (the Luhn method). Write a function cardCheckDigit(first15) that gets the first 15 digits as text and gives back the last digit as a number. How: start from the RIGHT of first15. Double the 1st, 3rd, 5th ... digit from the right; if a doubled value is more than 9, subtract 9. Add everything up. The check digit is (10 - total % 10) % 10. (These are made-up test numbers, not real cards.)

You will practice: check digits, Luhn method, loops from the right, remainder (%)

Mode

Press Tab to indent. Press Esc and then Tab to move on to the next button.

Your code runs in your browser. We do not receive it or save it.

Test results

      What your code printed

      
          

      CC BY-SA 4.0

      Adapted from a CS Unplugged programming challenge by the University of Canterbury Computer Science Education Research Group, licensed CC BY-SA 4.0.

      Original challenge · License: CC BY-SA 4.0

      Changes: Rewritten in our own words as a function with tests, hints and steps, in Python and JavaScript, by Open Cosmos. This version is also CC BY-SA 4.0.