Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Strings

    Hi,

    I need some help with this code. I want to make all the small letters to big and all the big letters to small letters. After that I want to make the letter "A" become an "#".

    It should look something like this:

    This Is a Text
    tHIS iS a tEXT
    This is # Text


    This is the code I currently work one:

    Code:
    	var str = "This Is a Text"
    	
    	if(str.toLowerCase == str) {
       		document.write(str.toUpperCase())
    		document.write("<br />")
    	}
    
    	if(str.toUpperCase == str) {
    		document.write(str.toLowerCase())
    		document.write("<br />")
    	}
    I am completely lost and need help or advice. Thanks!
    Last edited by zk0; 02-16-2007 at 03:22 PM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've updated the code a bit:

    Code:
    	var str = "This Is a Text"
    
    	if(str.toLowerCase == str) {
        str = str.toUpperCase();
        str = str.replace('A', '#');
        document.write(str);
        document.write("<br />");
    	}
    But it still has problems. Nothing is written onto the page!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    With respect to Kor, I don't see how regex will help here except for
    str = str.replace('A', '#');

    Here's what I have done so far, but still needs finishing off.

    <SCRIPT type="text/javascript">

    var str = "Hello world!"
    var flag = 0;
    var str1 = ""
    var a = new Array();
    for (var j=0; j<str.length; j++) {
    flag = 0; // reset the flag
    a[j] = (str.charCodeAt(j));
    if (a[j] >= 65 && a[j] <=90) {
    a[j] = a[j] + 32; // change upper case letter A-Z to lower case
    flag = 1; // set the flag
    }
    if (flag==0) { // only if flag not set
    if (a[j] >=97 && a[j] <=122) { // change lower case letter a-z to upper case
    a[j] = a[j] - 32;
    }
    }
    a[j] = "%" + a[j] // add % sign
    str1 = str1 + a[j] //concatenate
    alert (str1);

    }
    </script>

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    or
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function zxcES(zxcele,zxcstyle,zxcp,zxctxt){
     if (typeof(zxcele)=='string'){ zxcele=document.createElement(zxcele); }
     for (key in zxcstyle){ zxcele.style[key]=zxcstyle[key]; }
     if (zxcp){ zxcp.appendChild(zxcele); }
     if (zxctxt){ zxcele.appendChild(document.createTextNode(zxctxt)); }
     return zxcele;
    }
    
    function Cng(){
     var str = "This Is a Text"
     zxcES('DIV',{},document.getElementsByTagName('BODY')[0],str);
     str=str.split('');
     for (var zxc0=0;zxc0<str.length;zxc0++){
      if (str[zxc0]==str[zxc0].toUpperCase()){ str[zxc0]=str[zxc0].toLowerCase(); }
      else { str[zxc0]=str[zxc0].toUpperCase(); }
     }
     str=str.join('');
     zxcES('DIV',{},document.getElementsByTagName('BODY')[0],str);
     zxcES('DIV',{},document.getElementsByTagName('BODY')[0],str.replace(/A/g,'#'));
    }
    //-->
    </script></head>
    
    <body onload="Cng()">
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •