View Full Version : Javascript Strings
zk0
Feb 16th 2007, 7:23 am
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 />")
}
I am completely lost and need help or advice. Thanks!
MWilson
Feb 16th 2007, 7:25 am
Hi,
You already have the working code. What is it that you ask of me? :)
Mike
zk0
Feb 16th 2007, 7:31 am
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:
nico_swd
Feb 16th 2007, 7:47 am
if(str.toLowerCase == str) {
str = str.toUpperCase();
str = str.replace('A', '#');
document.write(str);
document.write("<br />");
}
zk0
Feb 16th 2007, 7:58 am
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 />");
}
But nothing is written onto the page with this code? :confused:
nico_swd
Feb 16th 2007, 8:14 am
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...
zk0
Feb 16th 2007, 8:30 am
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 />");
}
}
nico_swd
Feb 16th 2007, 9:10 am
No, look at this part.
if(str.toLowerCase == str) {
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.
Arnold9000
Feb 16th 2007, 10:15 am
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.
zk0
Feb 19th 2007, 2:31 am
Thank you, now I understand. :)
uce3feng3@hotmail.com
Feb 20th 2007, 1:04 am
I think you need a small program to finish this work,,
the code muse use "for "
zk0
Feb 22nd 2007, 7:07 am
Hi,
I want to create the following output:
Kalle Anka
kALLE aNKA
K#lle #nka
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:
Kalle Anka
kALLE aNKA :confused:
K#lle #nka
THANKS!
potnuru
Feb 22nd 2007, 11:40 am
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
}
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
That should do it i guess...
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.