Welcome to WebmasterWorld Guest from **184.72.177.182**

Forum
Moderators: **open**

I know nothing about javascript.

Assuming the string "3005600008000", I need to find a way to multiply all the digits in the odd numbered positions by 2 and the digits in the even numbered positions by 1.

This pseudo code I wrote outputs (I think) TRUE for the odd numbers (i.e. "0"),

` var camid;`

var LN= camid.length;

var mychar = camid.charAt(LN%2);

var arr = new Array(camid);

for(var i=0; i<arr.length; i++) {

var value = arr[i]%2;

Alert(i =" "+value);

}

I am not sure this is right: I don't believe it's chunking/splitting the string at odd (And later even) positions.

How do I that? Can you please provide some hints?

You are on the right track, using the modulus operator (%) with 2 to identify if a number is odd or even. If a number % 2 == 0, then the number is even.

Given a string, you can convert it to an array of characters using the split method:

var camid = "3005600008000",

arr = camid.split("");

Now you just need to iterate through the array:

var i, n, value;

for (i = 0, n = arr.length; i < n; i++) {

value = parseInt(arr[i], 10) * (i % 2 === 0? 1 : 2);

// do something with value

}

That will step through each item, and if the index is even, multiply by 1, else multiply by 2.

If, however, you actually need to chunk the values into a different array, then you would need to do a little bit more, but I'm not sure what you're trying to do.

I'm not too sure what you are wanting to output. Are you summing/joining the results together again to output another number?

Is the first position considered 'odd'? (Strings in JavaScript are indexed from 0, so without any additional processing the first character is essentially an 'even' position.)

To step through your string of digits (you have some bits kind of right)... assuming the first character position is actually an 'odd' character...

var camid = '3005600008000';

var mychar, value;

for (var i=1; i<=camid.length; i++) {

mychar = camid.charAt(i-1);

// Modulo operator (%) - returns remainder when divide by

if (i % 2 == 1) {

// Odd

value = mychar * 2;

} else {

// Even

value = mychar * 1;

}

// Alert popup for every character position

alert(i + ' = ' + value);

}

My goal is to implement a validation routine for a smartcard id number.

The logic I am trying to implement is as follows:

· 1) Starting from the left, multiply all the digits in the odd numbered positions by 2 and the digits in the even numbered positions by 1.

· 2) If the result of a multiplication of a single digit by 2 results in a two-digit number (say "7 x 2 = 14"), add the digits of the result together to produce a new single-digit result ("1+4=5").

· 3) Add all single-digit results together.

· 4) The check digit is the amount you must add to this result in order to reach the next highest multiple of ten. For instance, if the sum in step #3 is 22, to reach the next highest multiple of 10 (which is 30) you must add 8 to 22. Thus the check digit is 8.

That is the whole idea. Google searches on smartcard id validation returned nothing and I am beginning to think this is overkill to do this in Javascript...

Any input welcome.

Here's a working example that does exactly what you describe:

<!DOCTYPE html>

<html>

<head>

<title>Smartcard ID validation</title>

</head>

<body>

<pre>

3 0 0 5 6 0 0 0 0 8 0 0 0

* * * * * * * * * * * * *

2 1 2 1 2 1 2 1 2 1 2 1 2

6 0 0 5 12 0 0 0 0 8 0 0 0

6 +0 +0 +5 +3 +0 +0 +0 +0 +8 +0 +0 +0 = 22 (sum)

if 22 % 0 === 0, checksum = 0

else, checksum = 10 - (22 % 10) = 10 - 2 = 8

</pre>

<script>

var camid = "3005600008000",

arr = camid.split("");

var i, n, value, sum = 0;

for (i = 0, n = arr.length; i < n; i++) {

value = parseInt(arr[i], 10) * ((i + 1) % 2 === 0? 1 : 2);

if (value > 9) {

value = parseInt(value / 10, 10) + parseInt(value % 10, 10);

}

sum += value;

}

alert("sum = " + sum);

checkDigit = (sum % 10 === 0? 0: 10 - (sum % 10));

alert("check digit = " + checkDigit);

</script>

</body>

</html>

I discovered that what I am trying to do is a luhn validation.

I found an algorithm right here.

[sites.google.com...]

Thanks for taking the time. Much appreciated.

Note, the description in Wikipedia is slightly different from the one you gave. Basically, you start at the RIGHT of the string instead and work left. Here's the updated example:

<!DOCTYPE html>

<html>

<head>

<title>Luhn Validation</title>

</head>

<body>

<script>

function isLuhnValid(num) {

var arr = num.split(""),

i, n, value, sum = 0;

for (i = arr.length - 1, n = 0; i >= 0; i--, n++) {

value = parseInt(arr[i], 10) * ((n + 1) % 2 === 0? 2 : 1);

if (value > 9) {

value = parseInt(value / 10, 10) + parseInt(value % 10, 10);

}

sum += value;

}

return (sum % 10 === 0);

}

var data = [

'49927398716',

'49927398710'

];

alert(data[0] + ' is ' + (isLuhnValid(data[0])? '' : 'not ') + 'valid');

alert(data[1] + ' is ' + (isLuhnValid(data[1])? '' : 'not ') + 'valid');

</script>

</body>

</html>

For the record, the code that you linked to may be slightly more efficient, in the sense that it's not performing the multiplication/addition step. Essentially, since the possible data set is small (0,1,2,3,4,5,6,7,8,9), they've just pre-calculated the result of each when they fall in a place that does the multiplication:

0 = 0 * 2 = 0

1 = 1 * 2 = 2

2 = 2 * 2 = 4

3 = 3 * 2 = 6

4 = 4 * 2 = 8

5 = 5 * 2 = 10 = 1 + 0 = 1

6 = 6 * 2 = 12 = 1 + 2 = 3

7 = 7 * 2 = 14 = 1 + 4 = 5

8 = 8 * 2 = 16 = 1 + 6 = 7

9 = 9 * 2 = 18 = 1 + 8 = 9

- Register For Free! -
**Become a Pro Member!** - See forum categories - Enter the Forum

- Moderator List | Top Contributors:This Week, This Month, Jan, Dec, Archive, Top 100 All Time, Top Voted Members

- February 2016 AdSense Earnings and Observations
- Google Updates and SERP Changes - Feb 2016
- The Head Of Google Search, Amit Singhal Is Leaving The Company
- Google Updates and SERP Changes - Jan 2016
- Google Targeting Bad Ads With New Deceptive Site Label
- Google Webmaster Guidelines Updated, 2016
- John Mueller: Title tags "not the most critical part of a page"
- Facebook Opens Up Its Audience Network (Beta)
- Will moving content higher in source help rankings?
- Report: LinkedIn Stock Dropped $10-billion

- Report: LinkedIn Stock Dropped $10-billion
- Google Targeting Bad Ads With New Deceptive Site Label
- Facebook Says Its Version of Six Degrees of Separation is Three Point Five
- The Head Of Google Search, Amit Singhal Is Leaving The Company
- Google Updates and SERP Changes - Feb 2016
- Google Webmaster Guidelines Updated, 2016
- Yahoo To Cut 15pct of Staff and Close Some International Offices
- E.U. and U.S. Agree New Data Transfer Deal Replacing Safe Harbor
- Alphabet Q4 and Year End, 18pct Up to $21.329 Billion
- Where to Place WordPress htaccess Directives