Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 7th, 2008, 05:38 PM
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Invert letter case

Hi, I'm trying to invert the cases of a string using charCodeAt to determine if it's already uppercase or lowercase so I would be able to convert it.

This is what I have going on, but it's not exactly working.

<html>

<head>
<title>charCodeAt() - Ex. 3</title>

<script language="javascript" type="text/javascript">

var textodoom;

textodoom = new String("This is Josephine's string");

var textodoom_array = textodoom.split(" ");

var charCheck;

function inverttext() {

    for (i=0; i<textodoom_array.length; i++) {

        charCheck = textodoom_array.charCodeAt(i);

        if ((charCheck > 64) && (charCheck < 91)) {

            var lowerInvert = (textodoom_array(i)).toLowerCase();

        }

        if ((charCheck > 64) && (charCheck < 91)) {

            var upperInvert = (textodoom_array(i)).toUpperCase();

        }

        if (i >= textdoom_array.length) {

            for (j=0; j<textodoom_array.length; j++) {

                document.write(textodoom_array(j));
            }

        }

    }

}

</script>

</head>

<body>

<form name="buttonForm">

<p id="fancytext">This is Josephine's string</p>

<input type="button" name="imabutton" onClick="inverttext();" value="Click to invert string." />

<p id="othertext">&nbsp;</p>

</form>

</body>

</html>

Can someone tell me what I'm doing wrong?

Reply With Quote
  #2 (permalink)  
Old June 7th, 2008, 08:44 PM
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've gotten a little bit ahead.

<html>

<head>
<title>charCodeAt() - Ex. 3</title>

<script language="javascript" type="text/javascript">

var textodoom;

textodoom = "This is Josephine's string";

var charCheck;

var upperInvert = 0;

var lowerInvert = 0;

var letArray = new Array();

var otArray = new Array();

function inverttext() {

    for (i=0; i<=(textodoom.length-1); i++) {

        charCheck = textodoom.charCodeAt(i);

        if ((charCheck > 64) && (charCheck < 91)) {

            lowerInvert = (textodoom[i]).toLowerCase();

      var lowerRep = (textodoom[i]).replace((textodoom.charAt(i)),lowerInvert);

        }

        if ((charCheck > 96) && (charCheck < 123)) {

            upperInvert = (textodoom[i]).toUpperCase();

      var upperRep = (textodoom[i]).replace((textodoom.charAt(i)),upperInvert);

        }

    document.write(lowerRep + upperRep);

   }



}

</script>

</head>

<body>

<form name="buttonForm">

<p id="fancytext">This is Josephine's string</p>

<input type="button" name="imabutton" onClick="inverttext();" value="Click to invert string." />

<p id="othertext">&nbsp;</p>

</form>

</body>

</html>

When I run the function, it gives me:

tundefinedtHtItStStItStSjSjOjSjEjPjHjIjNjEjEjSjSjS jTjRjIjNjG

How would I be able to stop it from doing that and actually displaying the sentence?

Reply With Quote
  #3 (permalink)  
Old June 8th, 2008, 01:07 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii martoufmarty!!

Hope this will help you

<script>




function my()
{

textodoom = "This is Josephine's string";
str="";
for (i=0; i<textodoom.length; i++)
{
 currStr=textodoom.charAt(i)
 mystr=(currStr==currStr.toUpperCase())?currStr.toL owerCase():currStr.toUpperCase();
 str=str+""+mystr
 }
alert("final=="+str)
 }

</script>



<body>


<p id="fancytext">This is Josephine's string</p>

<input type="button" name="imabutton" onclick="my();" value="Click to invert string." >



Cheers :)

vinod
Reply With Quote
  #4 (permalink)  
Old June 8th, 2008, 06:59 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You can do that somewhat more compactly...

function my(inString)
{
    var outString = "";
    for( var i=0; i < inString.length; ++i )
    {
        var ch = inString.charAt(i);
        outString += ( ch >= 'A' && ch <= 'Z' ) ? ch.toLowerCase() : ch.toUpperCase();
    }
    return outString;
}
Reply With Quote
  #5 (permalink)  
Old June 8th, 2008, 08:45 PM
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for all your help, guys. I figured out what I was doing. Got it working. Yay! :D

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Letter replace in hangman. martoufmarty Javascript How-To 12 June 17th, 2008 12:30 AM
search first letter keyvanjan Classic ASP Basics 2 May 17th, 2006 01:47 AM
search string either Upper case or lower case rylemer Beginning VB 6 3 March 24th, 2004 04:23 PM
Check Case in a Case-Insensitive DB nbryson SQL Language 1 January 23rd, 2004 07:36 AM



All times are GMT -4. The time now is 05:09 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.