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!
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 "#".
if(str.toLowerCase == str) { str = str.toUpperCase(); str = str.replace('A', '#'); document.write(str); document.write("<br />"); } Code (javascript):
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?
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...
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):
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.
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.
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? THANKS!
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...