Javascript Strings

Discussion in 'JavaScript' started by zk0, Feb 16, 2007.

  1. #1
    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:

    	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 />")
    	}
    Code (markup):
    I am completely lost and need help or advice. Thanks!
     
    zk0, Feb 16, 2007 IP
  2. MWilson

    MWilson Peon

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,
    You already have the working code. What is it that you ask of me? :)
    Mike
     
    MWilson, Feb 16, 2007 IP
  3. zk0

    zk0 Peon

    Messages:
    299
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the fast reply! :)

    Well the problem is that the code I got there doesnt work. It doesnt show me any text and I don't know how to make all the letter "A"'s in the text to be transformed to "#". :confused:
     
    zk0, Feb 16, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    
    if(str.toLowerCase == str) {
        str = str.toUpperCase();
        str = str.replace('A', '#');
        document.write(str);
        document.write("<br />");
    }
    
    Code (javascript):
     
    nico_swd, Feb 16, 2007 IP
  5. zk0

    zk0 Peon

    Messages:
    299
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you nico!

    
    var str = "This Is a Text"
    
    if(str.toLowerCase == str) {
    str = str.toUpperCase();
    str = str.replace('A', '#');
    document.write(str);
    document.write("<br />");
    }
    Code (markup):
    But nothing is written onto the page with this code? :confused:
     
    zk0, Feb 16, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    It's cause of your if() condition. Think about the logic, and think why it doesn't write. Is the condition really true? ;) It's simple...
     
    nico_swd, Feb 16, 2007 IP
  7. zk0

    zk0 Peon

    Messages:
    299
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You mean like something like this?

    
    var str = "This Is a Text"
    	
    while (true) {
    
    if(str.toLowerCase == str) {
    str = str.toUpperCase();
    str = str.replace('A', '#');
    document.write(str);
    document.write("<br />");
    }
    }
    Code (markup):
     
    zk0, Feb 16, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    No, look at this part.
    
    if(str.toLowerCase == str) {
    
    Code (javascript):
    Translation: If str (to lower case) equals str (Normal case) THEN write to page.

    Further example:

    if "this is a text" equals "This Is a Text" THEN write.

    Understand the logic and why it doesn't write? Remove the if() condition totally... I don't think it's needed.
     
    nico_swd, Feb 16, 2007 IP
  9. Arnold9000

    Arnold9000 Peon

    Messages:
    241
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If you want to reverse each upper case character to lower case and vice versa, you have to walk the string character by character. Then, when you're done, the code for switching A to # that you've been given is correct.
     
    Arnold9000, Feb 16, 2007 IP
  10. zk0

    zk0 Peon

    Messages:
    299
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you, now I understand. :)
     
    zk0, Feb 19, 2007 IP
  11. uce3feng3@hotmail.com

    uce3feng3@hotmail.com Guest

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I think you need a small program to finish this work,,


    the code muse use "for "
     
    uce3feng3@hotmail.com, Feb 20, 2007 IP
  12. zk0

    zk0 Peon

    Messages:
    299
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hi,

    I want to create the following output:
    I know how to create "Kalle Anka" and "K#lle #nka".

    But how do I do to change the lowercase letters to uppercase and the uppercase letters to lowercase? :confused:

    THANKS!
     
    zk0, Feb 22, 2007 IP
  13. 87654321

    87654321 Well-Known Member

    Messages:
    317
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #13
    You have to march through each letter of the string using a for loop like this:
    
    for (i = 0;i < string.length; i++){
    var c = string.charAt(i);
    
    // Add the rest here
    
    }
    
    Code (markup):
    Then, check whether variable 'c' is uppercase or lowercase and then convert it to lowercase. And don't forget to rebuild your string using something like:
    
    var returnString += c
    
    Code (markup):
    That should do it i guess...
     
    87654321, Feb 22, 2007 IP