Welcome to WebmasterWorld Guest from **54.145.144.101**

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

- Bing App Indexing SEO
- Yandex Releases Beta Version Of Its Browser, With Privacy Turned On By Default
- LogJam Encryption Algo May Block Thousands of HTTPS Sites
- Google Webmaster Tools Renamed to Search Console
- Twitter's Content Now In U.S. Google Mobile SERPs
- Google Confirms Algo Change: "Quality Update"
- Google Rich Snippets Data Dropping Out Of SERPs
- Report: Google to Test Buy Buttons In Mobile SERPs
- Report: Mobile Networks In Europe Set To Block Google Ads
- Bing to Roll Out Mobile Friendly Algorithm

- Google Confirms Algo Change: "Quality Update"
- May 2015 AdSense Earnings and Observations
- 2015 Google On-page SEO Ranking Factors List (Including Deprecated Factors)
- Google Updates and SERP Changes - May 2015
- Bing App Indexing SEO
- Google Image Hijacking
- LogJam Encryption Algo May Block Thousands of HTTPS Sites
- Google Webmaster Tools Renamed to Search Console
- Google Rich Snippets Data Dropping Out Of SERPs
- Server Farms - February 2015

- Google Mobile Algo Roll-Out April 21, 2015
- May 2015 AdSense Earnings and Observations
- Google Confirms Algo Change: "Quality Update"
- April 2015 AdSense Earnings and Observations
- Google Updates and SERP Changes - May 2015
- Google Updates and SERP Changes - April 2015
- 2015 Google On-page SEO Ranking Factors List (Including Deprecated Factors)
- Google Switches Mobile Search To Show Real-World Names
- Google announces 1st quarter AdSense numbers
- Influence of Brand and Usage on Google Serps