The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Every other character as Uppercase
Terminator
post May 15 2016, 03:01 PM
Post #1


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



Do you know how to do this in JavaScript so it shows every other character as Uppercase?

http://codepad.org/9LC3SzjC

This would actually go off of what the user inputted though, so it would not have 'test example' in the code.

User can only enter letters per another regex code, but I'm not sure how to finish this below to make it toUpperCase and toLowerCase for every other letter.

CODE

var upperlower = str.replace(/\w........);


The code below is something else that makes every other character as a #

I can possibly use this as well by starting off with making the whole string lowercase as I did in the 3rd example, and then the for loop would make every other letter uppercase.

CODE

// replace every other character w/ #
function replaceEO(str) {
    var reo = str.split('');
    for (var i = 0; i < reo.length; i += 2) {
        reo[i] = '#';
    }
    return reo.join('');
};


But I wasn't sure how to add the toUpperCase.

CODE

// uppercase and lowercase mix
function upperLower(str) {
    var ulm = str.toLowerCase().split('');
    for (var i = 0; i < ulm.length; i += 2) {
        ulm[i] =............. wasn't sure how to finish this part................
    }
    return ulm.join('');
};
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post May 15 2016, 03:28 PM
Post #2


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
Joined: 9-August 06
Member No.: 6



Maybe there are neater ways, but I'd combine indexOf() with the modulus operator. Then you get what characters are on odd and even index spots in the string and can do your case conversion on them.

https://developer.mozilla.org/en-US/docs/We.../String/indexOf
http://stackoverflow.com/a/16929973
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 15 2016, 04:28 PM
Post #3


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Terminator @ May 15 2016, 10:01 PM) *

But I wasn't sure how to add the toUpperCase.

This should work:

CODE
var letter_arr = str.split('');
for (var i=0; i<letter_arr.length; i += 2)
{
    letter_arr[i]=letter_arr[i].toUpperCase();
}

pandy's idea sounds much neater, but frankly I have no idea how it's done. Modulus has always scared me. blush.gif

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Terminator
post May 15 2016, 04:52 PM
Post #4


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



QUOTE(Christian J @ May 15 2016, 04:28 PM) *

This should work:

CODE
var letter_arr = str.split('');
for (var i=0; i<letter_arr.length; i += 2)
{
    letter_arr[i]=letter_arr[i].toUpperCase();
}

pandy's idea sounds much neater, but frankly I have no idea how it's done. Modulus has always scared me. blush.gif


Thanks, this worked great. All I was really missing was finishing within the for loop:

CODE

ulm[i] = ulm[i].toUpperCase();


I knew I was very close on this one so I did not try the modulus way.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post May 15 2016, 09:33 PM
Post #5


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
Joined: 9-August 06
Member No.: 6



QUOTE(Christian J @ May 15 2016, 11:28 PM) *

pandy's idea sounds much neater, but frankly I have no idea how it's done. Modulus has always scared me. blush.gif


It may sound neat, but it was really stupid! laugh.gif

I always confuse indexOf() and charAt(). The latter is what would work and modulus isn't needed at all. blush.gif

The same loop as Terminator already use could be used with charAt(). If the string should start with a capital let it run from 0 to sting.lenght - 1 and increment with 2.

https://developer.mozilla.org/en-US/docs/We...s/String/charAt
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 29th March 2024 - 08:49 AM