?
Solved

Problem with Javascript function

Posted on 2005-03-14
5
Medium Priority
?
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

777 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