?
Solved

Problem with Javascript function

Posted on 2005-03-14
5
Medium Priority
?
853 Views
Last Modified: 2012-05-05
Dear All,

I am having problems with the following function. The function is supposed to take a sting of 'Ab' and return it back as 'aB' Instead this function for some reason is returning the string as 'aBb' I know that it is returning part of the original string but I have had no luck whats so ever in fixing this. Can aonyone help me spot the error. the code is as follows:

function isLowerCase(aCharacter)

/*
* Function returns true if its single-character string
* argument is a lowercase alphabetic character.
* Required for implementation of reverseCase.
*/

{
      return (aCharacter >= 'a') && (aCharacter <= 'z')
}



function reverseCase(aString)

/*
* Function returns
* a new string which has the same
* characters as aString except that all
* lowercase characters are replaced by
* their uppercase equivalents, and all
* uppercase characters are replaced by
* their lowercase equivalents.
*/

{
      var newString;       //the string to be returned

      newString = '';      //initialise the string to be returned
      for (var index = 0; index < aString.length; index = index + 1)
      {
            if (isLowerCase(aString.charAt(index)))
            {
                  newString = newString + aString.charAt(index).toUpperCase()
            }
            
                  newString = newString + aString.charAt(index).toLowerCase()
            
      };
      return newString
}


Many Thansk to anyone who can help!!


0
Comment
Question by:tbathgate
5 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 1340 total points
ID: 13533365
<script>
function isLowerCase(aCharacter)

/*
* Function returns true if its single-character string
* argument is a lowercase alphabetic character.
* Required for implementation of reverseCase.
*/

{
     return (aCharacter >= 'a') && (aCharacter <= 'z')
}



function reverseCase(aString)

/*
* Function returns
* a new string which has the same
* characters as aString except that all
* lowercase characters are replaced by
* their uppercase equivalents, and all
* uppercase characters are replaced by
* their lowercase equivalents.
*/

{
     var newString;      //the string to be returned

     newString = '';     //initialise the string to be returned
     for (var index = 0; index < aString.length; index++)
     {
          if (isLowerCase(aString.charAt(index)))
          {
               newString = newString + aString.charAt(index).toUpperCase()
          }else newString = newString + aString.charAt(index).toLowerCase()
         
     };
     return newString
}
</script>
0
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 1340 total points
ID: 13533376
You missed an "else"  in the reverseCase function:



 if (isLowerCase(aString.charAt(index)))
          {
               newString = newString + aString.charAt(index).toUpperCase()
          }else newString = newString + aString.charAt(index).toLowerCase(); // I ADDED THE else here.
0
 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 660 total points
ID: 13533388
function reverseCase(aString)

/*
* Function returns
* a new string which has the same
* characters as aString except that all
* lowercase characters are replaced by
* their uppercase equivalents, and all
* uppercase characters are replaced by
* their lowercase equivalents.
*/

{
     var newString;      //the string to be returned

     newString = '';     //initialise the string to be returned
     for (var index = 0; index < aString.length; index = index + 1)
     {
          if (isLowerCase(aString.charAt(index)))
          {
               newString = newString + aString.charAt(index).toUpperCase()
          }
          else
          {        
               newString = newString + aString.charAt(index).toLowerCase()
          }
     };
     return newString
}
0
 

Author Comment

by:tbathgate
ID: 13533687
Thanks all for the speedy and most helpful responses. I can't believe  I was silly enough to forget the else statement(I am only just learning javascript to explain my simple mistake)!!!! I suppose the saying a fresh pair of eyes is very true!!!

Thanks Again

Tom
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 13533726
You can forget the "else" statement.
Check this:

<script>

function strInvert(theStr){
  return theStr.replace(/\w/g,function(p){return(p<"a")?p.toLowerCase():p.toUpperCase()});
}


alert(strInvert("  aB cD "));


</script>

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question