How do you find proximal letters using JavaScript?
July 26th, 2011
I have to create a code-cracker in JavaScript. I’m having problems creating a loop or function to search for a certain letter, but collect the data for the letters directly adjacent to it. For example:
HPXQ RSTV BXPY
If doing a search for the letter P, I need to collect what letter is directly before and after each P in the code. In this case it would be H,X and X,Y.
How would I go about doing this with JavaScript?


I don’t know Javascript, but this should help:
Firstly set the prevletter variable to the first letter, and the nextletter variable to the third letter. If prevletter is P, then the second letter is the letter after P. Otherwise iterate through each letter from letter 2 to the end, and keep prevletter and nextletter at their respective places (current index +- 1) except when you are at the end, etc. When current letter is a P, store prevletter and nextletter in an array, or something like that.
Purr
You could use a regular expression:
var a = “HPXQ RSTV BXPY”;
var letter = “P”;
var pattern = new RegExp(”.” + letter + “.”, “g”);
var b = a.match(pattern);
will return b as an array of strings: “HPX”, “XPY”
You could then loop through the array with the split function:
for (var i=0; i
d = b.split (”P”);
..
// d(0) and d(1) are the letters you want for each occurrence of P
..
}