Welcome to WebmasterWorld Guest from 54.227.83.19

Forum Moderators: open

Message Too Old, No Replies

Chunk a string every odd and even position

     

kristo5747

11:26 pm on Jun 20, 2011 (gmt 0)

5+ Year Member



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?

Fotiman

12:36 am on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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

12:51 am on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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

2:16 am on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Whoops, my bad not catching that. :)

kristo5747

4:00 pm on Jun 21, 2011 (gmt 0)

5+ Year Member



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

4:58 pm on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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>

kristo5747

5:51 pm on Jun 21, 2011 (gmt 0)

5+ Year Member



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.

Fotiman

5:58 pm on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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

6:25 pm on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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>

Fotiman

6:30 pm on Jun 21, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month