|
|||||
Javascript: Letter incrementation à la PHP/Perl – Part 2Previously. on Javascript: Letter incrementation à la PHP/Perl - Part 1 we saw how to make a counter using strings. In this post I will show you how to use the the function from Part 1 to increment an arbitrary string. This still needs some tweaking regarding upper- and lowercase letters, and decrementing doesn't really work like a charm, so I'll stick to incrementing for now... Stay tuned for updates to this post.
function strIncr(str) {
var charStringU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // either do upperCase
var charStringL = 'abcdefghijklmnopqrstuvwxyz'; // or lowerCase
var isUpper='';
var myArray = str.split(''); // Separate the string letters into the array
for (i=0;i<myArray.length;i++) {
if (charStringU.indexOf(myArray[i]) == -1){ // See if it's an upper- or lowercase letter
myArray[i] = charStringL.indexOf(myArray[i]); // check the appropriate index;
}
else {
myArray[i] = charStringU.indexOf(myArray[i]);
isUpper=1; // set the Upper variable for later
}
}
var charString='';
if (isUpper) {
charString = charStringU; // pass Upper or Lower charString
}
else {
charString = charStringL;
}
return stringCounter(charString,myArray); // Call StringCounter
}
The Explanation: Lines 3 to 5: Initialize the ranges for upper- and lowercase characters. Also initialize the is_upper variable for usage down on line 21. Line 7: Split the string into individual letters and assign each letter into an array element. Line 9: Initialize an iteration through the array of letters. Lines 11 through 18: Here we check if the element exists in the upperCase charString or lowerCase charString. We then convert the array value into a number returned by indexOf. If the letter was found in the upperCase charString, it sets the is_upper variable. Lines 20 to 26: Initializes the charString array. It will hold either charStringU or charStringL, depending on the value of is_upper. Line 28: Calls function stringCounter passing it the charString we're using and also the array containing our string elements translated into numbers. stringCounter returns the calculated value to our current function which returns it to the caller. To see the code in action, use the following HTML/Javascript:
<html>
<head>
<script language="javascript" type="text/javascript">
function stringCounter(charString,charArray) {
if (charArray.length == 0) {
return "Invalid string!";
}
var str='';
var maxVal = charString.length-1;
// increment greatest index
++charArray[charArray.length-1];
// if greatest index == maxVal+1
if (charArray[charArray.length-1] == maxVal+1) {
// iterate from max to first and increment
for (i=charArray.length;i>0;i--){
if (charArray[i] == maxVal+1) {
charArray[i-1]++; // increment previous character
charArray[i]=0; // reset current character
}
}
}
// if first index surpasses the available sting, then
// reset whole counter and add one char
if (charArray[0] == maxVal+1) {
for (i=0;i<charArray.length;i++) {
charArray[i] = 0; // zeros counter on all indexes
}
charArray[charArray.length]=0; // appends a new index
}
// Prepare output by translating numbers and
// contatenating results
for (i=0;i<charArray.length;i++) {
str += charString.charAt(charArray[i]);
}
return str;
}
function strIncr(str) {
var charStringU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var charStringL = 'abcdefghijklmnopqrstuvwxyz';
var isUpper='';
var myArray = str.split('');
for (i=0;i<myArray.length;i++) {
if (charStringU.indexOf(myArray[i]) == -1){
myArray[i] = charStringL.indexOf(myArray[i]);
}
else {
myArray[i] = charStringU.indexOf(myArray[i]);
isUpper=1;
}
}
var charString='';
if (isUpper) {
charString = charStringU;
}
else {
charString = charStringL;
}
return stringCounter(charString,myArray);
}
</script>
</head>
<body>
<form>
<input type="text" id="string_input" size="20">
<input type="button" value="Go!"
onClick="alert(strIncr(document.getElementById('string_input').value));">
</form>
</body>
</html>
2 comments to Javascript: Letter incrementation à la PHP/Perl – Part 2 |
|||||
|
Copyright © 2010 use strict ;#) - All Rights Reserved |
|||||
Hi there Vinny!
Very interesting post! Just for my curiosity… LOL why did you need such kind of function in JS? Last year I used one very similar to this one you made, but in C ANSI. I was working on a university project under “distributed systems” classes.. actually it was a brute force password braker for MD5-crypted passwords. In a nutshell, it needed to generate all the passwords possibilities to try to break it, but the most important of this project, was that it needed to be ran on a cluster made with some computers, so, all the job needed to be breaked, devided and syncronized between all avaliable machines, so we could decrease the overall time to break a passwd.. well, this was my case, that’s why I am curious about yours! LOL.. Congratulations for this post, cya! []‘s
Hey Rodrigo!
Thanks for your comment. I’m working on a portal where the user selects some items from the database and gets to insert new ones in case it’s not found. Both kinds go into the same html table and I had to find a way to iterate through them separately. So for the ones that the user pulled from the database I used the MySQL table id, and for the new ones I used the alphabetical counter. Nothing as fun as breaking a password, but necessary nevertheless.