Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

javascript - Finding uppercase characters within a string

I am trying to write a function that decryptes an encrypted message that has uppercase letters (showing its a new word) and lower case characters (which is the word itself). The function needs to search through the encrypted message for all the uppercase letters and then returns the uppercase character along with lower case that follows it. I have been given a function to call on within the decrypt function:

function isUpperCase(aCharacter)    
{    
    return (aCharacter >= 'A') && (aCharacter <= 'Z');
}

I was thinking that I would search through the word for all the uppercase characters first and assign that as a new string. I could then do while loop that will pick up each of the letters in the new string and then search for the lower case characters that are next to it in the old string.

However, I am completely stuck at the first part - I cant even work out the structured English.

The code is:

  • encryptMessage is a string containing uppercase and lowercase characters
  • indexCharacter is used at a later date for another function
  • upperAlphabet - alphabet of uppercase characters - used later
  • lowerAlphabet - alphabet lowercase characters - used later

The function:

function decryptMessage(encryptMessage, indexCharacter, upperAlphabet, lowerAlphabet)
{
    var letter
    var word = "";

    for (var count = 0; count < encryptMessage.length; count = count +1);
    {
        letter = encryptMessage.charAt(count) 
        if (isUpperCase(letter));
        { 
            word = word + letter;       
        }
        document.write(word); //this is just to test to see if it returns the uppercase - I would use the return word
    }

The above just doesnt seem to work, so I cant even continue with the rest of the code. Can anyone help me identify where i have gone wrong - have I completely gone the wrong direction with this anyway, reading it back I dont think it really makes much sense ?? Its a very basic code, I have only learnt, for, while loops - if and else functions really, i am just soooooo stuck.

thanks in advance for your advice :-)

Issy

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not too sure I follow, but you can strip using the replace method and regular expressions

var str = 'MaEfSdsfSsdfsAdfssdGsdfEsdf';
var newmsg = str.replace(/[a-z]/g, '');
var old = str.replace(/[A-Z]/g, '');

In this case, newmsg = 'MESSAGE'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...