Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chunk a string every odd and even position

Status
Not open for further replies.

kristo5747

Programmer
Mar 16, 2011
41
US
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"),
Code:
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?
 
4 things to keep in mind.
1. your camid variable has no value. You must assign it a value if you want to perform operations on it.

Code:
var camid="value goes here";

2. in Javascript strings can be directly accessed as arrays. So no special transformation is necessary.

3. You'll need to somehow check that the current position is even or odd, so you would need to perform the check on the i variable.
The percentage character "%" can be used to obtain the remainder of a division, since all even numbers are divisible by 2 their remainder is 0. If the remainder is different than 0 its an odd number otherwise its an even number.

Code:
for(var i=0; i<camid.length; i++) {
   if(i[red]%[/red]2!=0){
   
   }
   else{
   
   }

}

4. Where are you wanting to store the results. You can build a new array to store the new numbers in it.

Code:
var results = new Array();
...

  if(i[red]%[/red]2!=0){
    results[i]=camid[i]*2
   }
...

Try to put it all together and see if it works for you.

PS: Javascript is case sensitive, so [red]A[/red]lert is different than [blue]a[/blue]lert. The function name is just alert();

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top