Welcome to WebmasterWorld Guest from 54.145.209.130 register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts, Pubcon Platinum Sponsor 2014
 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

# JavaScript and AJAX Forum

 Tweet
Chunk a string every odd and even position
kristo5747

Msg#: 4328515 posted 11:26 pm on Jun 20, 2011 (gmt 0)

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?

Fotiman

Msg#: 4328515 posted 12:36 am on Jun 21, 2011 (gmt 0)

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.

penders

Msg#: 4328515 posted 12:51 am on Jun 21, 2011 (gmt 0)

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); }`

Fotiman

Msg#: 4328515 posted 2:16 am on Jun 21, 2011 (gmt 0)

Whoops, my bad not catching that. :)

kristo5747

Msg#: 4328515 posted 4:00 pm on Jun 21, 2011 (gmt 0)

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.

Fotiman

Msg#: 4328515 posted 4:58 pm on Jun 21, 2011 (gmt 0)

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

 ``Smartcard ID validation
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 26 0 0 5 12 0 0 0 0 8 0 0 06 +0 +0 +5 +3 +0 +0 +0 +0 +8 +0 +0 +0 = 22 (sum)if 22 % 0 === 0, checksum = 0else, checksum = 10 - (22 % 10) = 10 - 2 = 8
``

kristo5747

Msg#: 4328515 posted 5:51 pm on Jun 21, 2011 (gmt 0)

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

I found an algorithm right here.

Thanks for taking the time. Much appreciated.

Fotiman

Msg#: 4328515 posted 5:58 pm on Jun 21, 2011 (gmt 0)

Personally, I wouldn't use the code that you linked to, as it modifies the built-in String object. I prefer not to inject my own code into other objects. In addition, I find that code much more difficult to follow.

Fotiman

Msg#: 4328515 posted 6:25 pm on Jun 21, 2011 (gmt 0)

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:

 ``Luhn Validation``

Fotiman

Msg#: 4328515 posted 6:30 pm on Jun 21, 2011 (gmt 0)

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

 Global Options: top home search open messages active posts  Tweet

 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX