Implement Luhn Algorithm in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the implementation of the Luhn Algorithm in JavaScript. The Luhn Algorithm is commonly used to validate identification numbers such as credit card numbers, IMEI numbers, and National Provider Identifier numbers. Through this lab, you will learn how to split a string, reverse an array, use map(), reduce(), and shift() methods to implement the Luhn Algorithm, and ultimately determine whether a given identification number is valid or not.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/str_manip("`String Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28477{{"`Implement Luhn Algorithm in JavaScript`"}} javascript/data_types -.-> lab-28477{{"`Implement Luhn Algorithm in JavaScript`"}} javascript/arith_ops -.-> lab-28477{{"`Implement Luhn Algorithm in JavaScript`"}} javascript/comp_ops -.-> lab-28477{{"`Implement Luhn Algorithm in JavaScript`"}} javascript/str_manip -.-> lab-28477{{"`Implement Luhn Algorithm in JavaScript`"}} javascript/higher_funcs -.-> lab-28477{{"`Implement Luhn Algorithm in JavaScript`"}} end

Luhn Check

To use the Luhn Algorithm for validation of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the following methods: String.prototype.split(), Array.prototype.reverse(), Array.prototype.map(), and parseInt() in combination to obtain an array of digits.
  3. Use Array.prototype.shift() to obtain the last digit.
  4. Use Array.prototype.reduce() to implement the Luhn Algorithm.
  5. Return true if sum is divisible by 10, false otherwise.

Here's the code:

const luhnCheck = (num) => {
  const arr = (num + "")
    .split("")
    .reverse()
    .map((x) => parseInt(x));
  const lastDigit = arr.shift();
  let sum = arr.reduce(
    (acc, val, i) =>
      i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val),
    0
  );
  sum += lastDigit;
  return sum % 10 === 0;
};

You can test the Luhn Check function using these examples:

luhnCheck("4485275742308327"); // true
luhnCheck(6011329933655299); //  true
luhnCheck(123456789); // false

Summary

Congratulations! You have completed the Luhn Check lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like