Ok, i have a function which checks if a letter is upper case and returns 'true' or 'false' value.
function isUpperCase(aCharacter) {
return (aCharacter >= 'A') && (aCharacter <= 'Z');
}
now I would like it to check a string of characters e.g. 'AdfdfZklfksPaabcWsgdf' and after the program encounters capital letter it will execute function decryptWord on all small letters after this letter and until next capital letter and so on. Function dec开发者_Go百科ryptWord works fine on single words i just cant get it work on more than one ;(
function decryptMessage(cipherText, indexCharacter, plainAlphabet, cipherAlphabet) {
for (var count = 0, count < cipherText.length; count++) {
if (isUpperCase(cipherText.charAt(count))) {
decryptWord(cipherText, indexCharacter, plainAlphabet, cipherAlphabet)
} else {
//i dont know what to do next
}
}
}
can you tell me if i'm going in the right direction?
Have you considered a regex that splits before each uppercase character? For instance
"AdfdfZklfksPaabcWsgdf".split(/(?=[A-Z])/);
Results in:
["Adfdf", "Zklfks", "Paabc", "Wsgdf"]
This way, you can manage one "word" at a time; the first character of each is always upper-case, the rest are lower-case.
Your isUpperCase
function returns false
for a space character, so the code treats space characters and lowercase characters the same. That's probably why it's barfing on multiple words but not single ones.
Instead of dealing with upper- and lower-case letters, why not split
the input on non-word characters? Something like this:
var words = cipherText.split(/\W/), // \W means non-word characters
numWords = words.length;
for (var i = 0; i < numWords; i++) {
decryptWord(words[i]);
}
I would recommend using two variables in the decryptMessage
function. The first variable, last_caps
, will store the index of the previous capitalized letter. The second variable is count
, which will work very similarly to how it does already. This allows you to know where the previous capital letter was, so when you find the next capital letter, you can use decryptWord
on the lowercase letters between them.
An iteration of the for loop
- If the character at index
count
is uppercase, then:- If
count - last_caps > 1
, then:- Use
decryptWord
to decrypt the substring starting atlast_caps
and ending atcount
. Manipulate the values as necessary to include/exclude the capital letters.
- Use
- Overwrite
last_caps
withcount
. (last_caps = count
)
- If
- End of
for
iteration.
JavaScript function to split on Capital Letters:
String.prototype.splitForCapitalLetters = function () {
var string = this;
if (string.length) {
string = string.split(/(?=[A-Z])/);
}
return string;
};
Call:
"AnyString".splitForCapitalLetters();
Result:
["Any", "String"]
columns is array of string and leave fully cases such as upper or lower case
function cipherText(columns) {
for (var i = 0; i < columns.length; i++) {
if (!isAnyCase(columns[i])) {
columns[i] = columns[i].split(/(?=[A-Z])/).join(" ");
}
}
return columns;
}
function isAnyCase(text) {
return (text == text.toUpperCase() || text == text.toLowerCase());
}
calling of function
var columns = cipherText(arr);
精彩评论