# Testing Luhn algorithm with JavaScript

**URL:** https://forum.kirupa.com/t/testing-luhn-algorithm-with-javascript/634468
**Category:** programming
**Created:** [April 14, 2016, 12:53pm UTC](https://forum.kirupa.com/t/testing-luhn-algorithm-with-javascript/634468 "2016-04-14T12:53:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![VillaRava](https://avatars.discourse-cdn.com/v4/letter/v/ee7513/32.png) [@VillaRava](https://forum.kirupa.com/u/VillaRava)
#### Post date: [April 14, 2016, 12:53pm UTC](https://forum.kirupa.com/t/testing-luhn-algorithm-with-javascript/634468/1 "2016-04-14T12:53:08Z")

</div>

I have created some JavaScript-code that tests a number according to the Luhn algorithm. It works the way it should. However, it is rather lengthy. I’m wondering if there’s a smart way to shorten the code? Sure, there are other ways to write the whole thing, but I do like this particular for-loop and the charAt() method, they are comprehensible to me. I would prefer to maintain the “tools” but still cut back on functions and possibly variables.

function luhnTest(userInput){

```
	var backwards = "";
	var multiplyx2 = "";
	var total = 0;
	
	/* For-loop going through the number. It begins at the second digit from the end.
	Then it adds to "backwards" every other digit going backwards through the number. */ 
	for(i = userInput.length-2; i >= 0; i-=2){
		backwards += userInput.charAt(i);
	}	
	
	/* For-loop going through the digits in "backwards" and multiplying them by 2. 
	The multiplied digits are stored in "multiplyx2".*/
	for(i = 0; i < backwards.length; i++){
		multiplyx2 += backwards.charAt(i)*2;
	}	
	
	/* The digits in "multiplyx2" are added up and stored in "total". */ 
	for(i = 0; i < multiplyx2.length; i++){
		total += parseInt(multiplyx2.charAt(i));
	}
	
	/* Adding up the digits that have been left out. Starting with the third digit from the end.
	These digits are added to "total". */
	for(i = userInput.length-3; i >= 0; i-=2){
		total += parseInt(userInput.charAt(i));
	}
	
	/* Adding the last digit in the card number to "total".
	Could have done so in previous loop but just for the sake of it (that's how the Luhn algorithm goes) . */
	total += parseInt(userInput.charAt(userInput.length-1))
	
	/* Testing if "total" can be divided by 10 without leaving a remainder. */		
	if((total % 10) == 0){
		return true;
	}	

```

}

---

<div class="post-metadata">

### Author: ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)
#### Post date: [April 15, 2016, 6:53pm UTC](https://forum.kirupa.com/t/testing-luhn-algorithm-with-javascript/634468/2 "2016-04-15T18:53:35Z")

</div>

Being readable all comes down to the individual coder.  
There are shorter Luhn javascripts on wikipedia, I happened to see them when I read up on the algorithm.

I like when things are readable, so I like this type of scripts that you have done.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 16, 2016, 3:26am UTC](https://forum.kirupa.com/t/testing-luhn-algorithm-with-javascript/634468/3 "2016-04-16T03:26:01Z")

</div>

I agree - readability totally trumps brevity unless there is a massive performance drawback or something 😛

That’s one of the reasons why I still don’t like arrow functions introduced as part of ES6 haha.
