UpperCase the even index of a string - javascript

i am trying to UpperCase() the even indexes of a string;
input: hello world;
output: HeLlO WoRlD;
somehow its not working, i think it is because i cannot mutate a string since it is returning the initial value. Can someone help me solve this problem ?
/* Alternating Caps
Write a function that takes in a string of letters
and returns a sentence in which every other letter is capitalized.
Example input: "I'm so happy it's Monday"
Example output: "I'M So hApPy iT'S MoNdAy"
*/
function altCaps(str){
let string = str
for(let i = 0; i < str.length; i++) {
if(i % 2 === 0) {
string[i].toUpperCase()
}
}
console.log(string)
}

Strings are immutable, you need to create a new string and append the letters one by one.
function altCaps(oldStr){
let newStr = '';
for(let i = 0; i < oldStr.length; i++)
newStr += i % 2 === 0 ? oldStr[i].toUpperCase() : oldStr[i];
return newStr;
}
console.log(altCaps("I'm so happy it's monday"))
Notice this isn't attempting to mutate newStr, it's getting a new string reassigned for every letter we append.

function altCaps(text,mode=0) {
return text.split('').map((c,i) =>
i % 2 == mode ? c.toLowerCase() : c.toUpperCase()
).join('');
}
console.log(altCaps("I'm so happy it's Monday",1));

The problem with your solution is that .toUpperCase() doesn't modify the string it's called on. Rather, it returns a new upper-cased string.
Here's one way the problem could be solved:
const test = "Hello World";
const toAltCase = s => s.split("")
.map((c, i) => (i % 2 != 0)?
c.toLowerCase() :
c.toUpperCase())
.join("");
console.log(toAltCase(test), "<-", test);

Related

Undefined in Split String

i have a function to split string into 2 part, front and back. Then reverse it to back and front. Here is my code
function reverseString(string) {
let splitString = ""
let firstString = ""
for(i = 0; i <= string.length/2 - 1; i++) {
firstString += string[i]
}
for(i = string.length/2; i <= string.length; i++) {
splitString += string[i]
}
return splitString + firstString
}
Sorry for bad explanation, this is test case and expected result (first one is expected result, the second one is my result)
console.log(reverseString("aaabccc")); // "cccbaaa" "undefinedundefinedundefinedundefinedaaa"
console.log(reverseString("aab")); // "baa" "undefinedundefineda"
console.log(reverseString("aaaacccc")); // "ccccaaaa" "ccccundefinedaaa"
console.log(reverseString("abcdefghabcdef")); // "habcdefabcdefg" "habcdefundefinedabcdefg"
could you help me, whats wrong with it. Thank you
You could try another approach and use the slice function
function reverseString(string)
{
if (string.length < 2) { return string; }
let stringHalfLength = string.length / 2;
let isLengthOdd = stringHalfLength % 1 !== 0;
if (isLengthOdd) {
return string.slice(Math.ceil(stringHalfLength), string.length + 1) + string[Math.floor(stringHalfLength)] + string.slice(0, Math.floor(stringHalfLength));
}
return string.slice(stringHalfLength, string.length + 1) + string.slice(0, stringHalfLength);
}
console.log(reverseString("aaabccc") === "cccbaaa");
console.log(reverseString("aab") === "baa");
console.log(reverseString("aaaacccc") === "ccccaaaa");
console.log(reverseString("abcdefghabcdef") === "habcdefabcdefg");
A more efficient way to reverse the string would be to split the string, then use the built-in reverse javascript function (which reverses the elements of the split string), and then re-join the elements using the join function.. No need to re-invent the wheel?
You can concatenate the functions in shorthand (.split.reverse.join etc...) so your function would look something like this:
function reverseString(string) {
return string.split("").reverse().join("");
}
Try it out!
function reverseString(string) {
return string.split("").reverse().join("");
}
console.log(reverseString("hello"));
console.log(reverseString("aaabbbccc"));
If there's a particular reason you're opting not to use the in-built functions (i.e. if I've missed something?) , feel free to comment.
The short version of what you need:
function reverseString(string) {
const splitPosition = Math.ceil(string.length / 2);
return string.substring(splitPosition) + string.substring(0, splitPosition);
}
The key to your question is the middle element. To accomplish that, you probably want to use Math.floor that round under.
console.log(reverseString("aaabccc")); // "cccbaaa"
console.log(reverseString("abcdefghabcdef")); // "habcdefabcdefg"
function reverseString (str) {
if (str.length<2) {
return str
}
var half = Math.floor(str.length / 2);
return (str.slice(-half) + (str.length%2?str[half]:"") + str.slice(0,half));
}
reverseString('')
> ""
reverseString('1')
> "1"
reverseString('12')
> "21"
reverseString('123')
> "321"
reverseString('1234')
> "3412"
reverseString('12345')
> "45312"
reverseString("aaabccc")
> "cccbaaa"
reverseString("abcdefghabcdef")
> "habcdefabcdefg"
So basically your problem is not to grab 2 parts of the string and rearrange, it is to grab 3 parts.
1 part: str.slice(0,half)
2 part: str.length%2 ? str[half] : ""
3 part: str.slice(-half)
The second part is empty if the string length is even and the middle character if is odd.
So the code version in long self explanatory code:
function reverseString (str) {
if (str.length<2) {
return str
}
var half = Math.floor(str.length / 2);
var firstPart = str.slice(0,half);
var midlePart = str.length % 2 ? str[half] : ""; // we could expand also this
var endPart = str.slice(-half);
return endPart + midlePart + firstPart;
}
And also, notice the precondition, so I don't have to deal with the easy cases.
Also, in your code, you got undefined because you access in the last loop to:
string[string.length] you need to change <= by <

How to move all capital letters to the beginning of the string?

I've been practicing simple solutions using what I've been learning / known.
The question I've faced is, how to move the capital letters in the string to the front?
I've solved it, but it's not to my expectation as my original idea was to → find the uppercase letter → put them in an array → concat the uppercase with the original string array with the uppercase letter removed in it.
Hence my question is, how can I remove the capital letter in the first conditional statement so I won't need to create another conditional statement to find the lower case and store the lower case letter in an array?
For example, the input string is 'heLLo' → output would be 'LLheo' (the capital letters are now in front).
Thank you!
function capToFront(s) {
var sp = s.split("");
var caps = [];
var lower = []
for (var i = 0; i < sp.length; i++)
{
if (sp[i] == sp[i].toUpperCase()){
caps.push(sp[i]);
**//How can i remove the capital letter in "sp" array as I've pushed them into the caps Array**
}
if (sp[i] == sp[i].toLowerCase()){
lower.push(sp[i]);
}
}
return caps.join("").concat(lower.join(""));
}
With RegExp, you can accomplish your goal in one line without any loops:
const result = [...'heLLo'].sort(l => /[A-Z]/.test(l) ? -1 : 0).join('');
console.log(result); // LLheo
If you want to ensure the original order among the capital letters is preserved, it will be slightly longer:
const result = [...'Hello World Foo Bar']
.sort((a, b) => /[A-Z]/.test(a) ? /[A-Z]/.test(b) ? 0 : -1 : 0)
.join('');
console.log(result); // HWFBello orld oo ar
You can reach your goal with a smaller loop by using Regex.
function capToFront(sp) {
let upperRgx = /[A-Z]/g;
let upperLetters = sp.match(upperRgx);
for(let i=0; i < upperLetters.length;i++) {
let indx = sp.indexOf(upperLetters[i]);
sp = sp.substring(0,indx)+sp.substring(indx+1,sp.length);
}
sp = upperLetters.join("")+sp;
return sp;
}
console.log(capToFront("heLLo")) // Output: LLheo
Use the Splice method to remove.
function capToFront(s) {
var sp = s.split("");
var caps = [];
var lower = []
for (var i = 0; i < sp.length; i++)
{
if (sp[i] == sp[i].toUpperCase()){
caps.push(sp[i]);
// Use the `splice` method to remove
sp.splice(i, 1);
}
if (sp[i] == sp[i].toLowerCase()){
lower.push(sp[i]);
}
}
console.log('sp', sp);
return caps.join("").concat(lower.join(""));
}
console.log(capToFront("stAck"))
You can also try this approach where you check the ASCII value of characters as the capital letters lie between 65 and 90 then use .sort and .join methods on the array accordingly
function capToFront(s) {
var sp = s.split("");
const res = sp.sort((a,b)=> isCaps(a) ? isCaps(b) ? 0 : -1 : 0)
return res.join("")
}
function isCaps(c){
return c.charCodeAt()>=65 && c.charCodeAt()<=90
}
console.log(capToFront('hIsAmplEStRing'))

How to get odd and even position characters from a string?

I'm trying to figure out how to remove every second character (starting from the first one) from a string in Javascript.
For example, the string "This is a test!" should become "hsi etTi sats!"
I also want to save every deleted character into another array.
I have tried using replace method and splice method, but wasn't able to get them to work properly. Mostly because replace only replaces the first character.
function encrypt(text, n) {
if (text === "NULL") return n;
if (n <= 0) return text;
var encArr = [];
var newString = text.split("");
var j = 0;
for (var i = 0; i < text.length; i += 2) {
encArr[j++] = text[i];
newString.splice(i, 1); // this line doesn't work properly
}
}
You could reduce the characters of the string and group them to separate arrays using the % operator. Use destructuring to get the 2D array returned to separate variables
let str = "This is a test!";
const [even, odd] = [...str].reduce((r,char,i) => (r[i%2].push(char), r), [[],[]])
console.log(odd.join(''))
console.log(even.join(''))
Using a for loop:
let str = "This is a test!",
odd = [],
even = [];
for (var i = 0; i < str.length; i++) {
i % 2 === 0
? even.push(str[i])
: odd.push(str[i])
}
console.log(odd.join(''))
console.log(even.join(''))
It would probably be easier to use a regular expression and .replace: capture two characters in separate capturing groups, add the first character to a string, and replace with the second character. Then, you'll have first half of the output you need in one string, and the second in another: just concatenate them together and return:
function encrypt(text) {
let removedText = '';
const replacedText1 = text.replace(/(.)(.)?/g, (_, firstChar, secondChar) => {
// in case the match was at the end of the string,
// and the string has an odd number of characters:
if (!secondChar) secondChar = '';
// remove the firstChar from the string, while adding it to removedText:
removedText += firstChar;
return secondChar;
});
return replacedText1 + removedText;
}
console.log(encrypt('This is a test!'));
Pretty simple with .reduce() to create the two arrays you seem to want.
function encrypt(text) {
return text.split("")
.reduce(({odd, even}, c, i) =>
i % 2 ? {odd: [...odd, c], even} : {odd, even: [...even, c]}
, {odd: [], even: []})
}
console.log(encrypt("This is a test!"));
They can be converted to strings by using .join("") if you desire.
I think you were on the right track. What you missed is replace is using either a string or RegExp.
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.
Source: String.prototype.replace()
If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g) modifier
Source: JavaScript String replace() Method
So my suggestion would be to continue still with replace and pass the right RegExp to the function, I guess you can figure out from this example - this removes every second occurrence for char 't':
let count = 0;
let testString = 'test test test test';
console.log('original', testString);
// global modifier in RegExp
let result = testString.replace(/t/g, function (match) {
count++;
return (count % 2 === 0) ? '' : match;
});
console.log('removed', result);
like this?
var text = "This is a test!"
var result = ""
var rest = ""
for(var i = 0; i < text.length; i++){
if( (i%2) != 0 ){
result += text[i]
} else{
rest += text[i]
}
}
console.log(result+rest)
Maybe with split, filter and join:
const remaining = myString.split('').filter((char, i) => i % 2 !== 0).join('');
const deleted = myString.split('').filter((char, i) => i % 2 === 0).join('');
You could take an array and splice and push each second item to the end of the array.
function encrypt(string) {
var array = [...string],
i = 0,
l = array.length >> 1;
while (i <= l) array.push(array.splice(i++, 1)[0]);
return array.join('');
}
console.log(encrypt("This is a test!"));
function encrypt(text) {
text = text.split("");
var removed = []
var encrypted = text.filter((letter, index) => {
if(index % 2 == 0){
removed.push(letter)
return false;
}
return true
}).join("")
return {
full: encrypted + removed.join(""),
encrypted: encrypted,
removed: removed
}
}
console.log(encrypt("This is a test!"))
Splice does not work, because if you remove an element from an array in for loop indexes most probably will be wrong when removing another element.
I don't know how much you care about performance, but using regex is not very efficient.
Simple test for quite a long string shows that using filter function is on average about 3 times faster, which can make quite a difference when performed on very long strings or on many, many shorts ones.
function test(func, n){
var text = "";
for(var i = 0; i < n; ++i){
text += "a";
}
var start = new Date().getTime();
func(text);
var end = new Date().getTime();
var time = (end-start) / 1000.0;
console.log(func.name, " took ", time, " seconds")
return time;
}
function encryptREGEX(text) {
let removedText = '';
const replacedText1 = text.replace(/(.)(.)?/g, (_, firstChar, secondChar) => {
// in case the match was at the end of the string,
// and the string has an odd number of characters:
if (!secondChar) secondChar = '';
// remove the firstChar from the string, while adding it to removedText:
removedText += firstChar;
return secondChar;
});
return replacedText1 + removedText;
}
function encrypt(text) {
text = text.split("");
var removed = "";
var encrypted = text.filter((letter, index) => {
if(index % 2 == 0){
removed += letter;
return false;
}
return true
}).join("")
return encrypted + removed
}
var timeREGEX = test(encryptREGEX, 10000000);
var timeFilter = test(encrypt, 10000000);
console.log("Using filter is faster ", timeREGEX/timeFilter, " times")
Using actually an array for storing removed letters and then joining them is much more efficient, than using a string and concatenating letters to it.
I changed an array to string in filter solution to make it the same like in regex solution, so they are more comparable.

Alternate capitalisation in a string

I would like to create a function which alternates letter capitalisation. For example, Hello World would become HeLlO wOrLd. The first letter of the string must always be capitalised.
Here is the code I have written:
function alternatingCaps(str) {
let alternate = str.charAt(0).toUpperCase();
for(let i = 1; i < str.length; i++) {
let previousChar = str.charAt(i - 1);
if(previousChar === previousChar.toUpperCase())
alternate += str.charAt(i).toLowerCase();
else if(previousChar === previousChar.toLowerCase())
alternate += str.charAt(i).toUpperCase();
}
return alternate;
}
I declared the alternate variable with the capitalised first character of the supplied string. I then loop through the rest of the string and check if the character preceding the current iteration is uppercase or lowercase; whichever it is, the current letter will become the opposite.
However, this does not have the desired outcome. Here are a couple of tests and their corresponding results:
console.log(alternatingCaps('hello world'));
// Output: "HELLO wORLD"
console.log(alternatingCaps('jAvaScrIPT ruLEZ'));
// Output: "JAvAScRIpt rULez"
How do I fix my function?
let s = 'hello there this is a test';
s = s.split('').map( (letter,i) => (i % 2) == 0 ? letter.toUpperCase() : letter.toLowerCase() ).join('')
console.log( s );
Update: if you want to ignore but preserve the spaces, then here is another solution, albeit a little screwy. It doesn't just ignore spaces, it only operates on letters matching the regular expression.
let s = 'hello there this is a test';
let ul = false;
s = s.split('').map(letter => letter.match(/[a-zA-Z]/) != null ? (ul = ! ul, ul ? letter.toUpperCase() : letter.toLowerCase()) : letter).join('');
console.log( s );
Simplest solution i can think of is using for loop and Remainder operator.
let alterNate = (input) => {
let str = ''
let last = 'L'
for(let i=0; i<input.length; i++){
if( /[a-z]/ig.test(input[i]) ) {
if(last==='L'){
str+= input[i].toUpperCase()
last = 'U'
} else{
str+= input[i].toLowerCase()
last = 'L'
}
} else {
str+=input[i]
}
}
return str;
}
console.log(alterNate('hello world'))
My solution uses RegEx replace to achieve the expected result:
function alternatingCaps(str) {
return str.replace(/\w(.|$)/g, s => s[0].toUpperCase() + (s[1] ? s[1].toLowerCase() : ''));
}
console.log(alternatingCaps('hello world'));
console.log(alternatingCaps('jAvaScrIPT ruLEZ'));
There are two things to pay attention for:
\w(.|$) - This regular expression captures a word character (\w) and any other following char. The (.|$) address for an odd number of characters. We're basically capturing the characters in pairs here;
s => s[0].toUpperCase() + (s[1] ? s[1].toLowerCase() : '') - This replacing function does the rest of the job:
s[0].toUpperCase() - It takes s[0] and changes it to uppercase;
(s[1] ? s[1].toLowerCase() : '') - And it takes s[1], if it exists, and changes it to lowercase.
Here is the sample regex for alternating case
[A-Z]([a-z][A-Z])*[a-z]?
https://regex101.com/r/UPHRUk/1

Swap Case on javascript

I made a script that changes the case, but result from using it on text is exactly the same text, without a single change. Can someone explain this?
var swapCase = function(letters){
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){
letters[i] = letters[i].toUpperCase();
}else {
letters[i] = letters[i].toLowerCase();
}
}
console.log(letters);
}
var text = 'So, today we have REALLY good day';
swapCase(text);
Like Ian said, you need to build a new string.
var swapCase = function(letters){
var newLetters = "";
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){
newLetters += letters[i].toUpperCase();
}else {
newLetters += letters[i].toLowerCase();
}
}
console.log(newLetters);
return newLetters;
}
var text = 'So, today we have REALLY good day';
var swappedText = swapCase(text); // "sO, TODAY WE HAVE really GOOD DAY"
You can use this simple solution.
var text = 'So, today we have REALLY good day';
var ans = text.split('').map(function(c){
return c === c.toUpperCase()
? c.toLowerCase()
: c.toUpperCase()
}).join('')
console.log(ans)
Using ES6
var text = 'So, today we have REALLY good day';
var ans = text.split('')
.map((c) =>
c === c.toUpperCase()
? c.toLowerCase()
: c.toUpperCase()
).join('')
console.log(ans)
guys! Get a little simplier code:
string.replace(/\w{1}/g, function(val){
return val === val.toLowerCase() ? val.toUpperCase() : val.toLowerCase();
});
Here is an alternative approach that uses bitwise XOR operator ^.
I feel this is more elegant than using toUppserCase/ toLowerCase methods
"So, today we have REALLY good day"
.split("")
.map((x) => /[A-z]/.test(x) ? String.fromCharCode(x.charCodeAt(0) ^ 32) : x)
.join("")
Explanation
So we first split array and then use map function to perform mutations on each char, we then join the array back together.
Inside the map function a RegEx tests if the value is an alphabet character: /[A-z]/.test(x) if it is then we use XOR operator ^ to shift bits. This is what inverts the casing of character. charCodeAt convert char to UTF-16 code. XOR (^) operator flips the char. String.fromCharCode converts code back to char.
If RegEx gives false (not an ABC char) then the ternary operator will return character as is.
References:
String.fromCharCode
charCodeAt
Bitwise operators
Ternary operator
Map function
One liner for short mode code wars:
let str = "hELLO wORLD"
str.split("").map(l=>l==l.toLowerCase()?l.toUpperCase():l.toLowerCase()).join("")
const swapCase = (myString) => {
let newString = ''; // Create new empty string
if (myString.match(/[a-zA-Z]/)) { // ensure the parameter actually has letters, using match() method and passing regular expression.
for (let x of myString) {
x == x.toLowerCase() ? x = x.toUpperCase() : x = x.toLowerCase();
newString += x; // add on each conversion to the new string
}
} else {
return 'String is empty, or there are no letters to swap.' // In case parameter contains no letters
}
return newString; // output new string
}
// Test the function.
console.log(swapCase('Work Today Was Fun')); // Output: wORK tODAY wAS fUN
console.log(swapCase('87837874---ABCxyz')); // Output: 87837874---abcXYZ
console.log(swapCase('')); // Output: String is empty, or there are no letters to swap.
console.log(swapCase('12345')); // Output: String is empty, or there are no letters to swap.
// This one will fail. But, you can wrap it with if(typeof myString != 'number') to prevent match() method from running and prevent errors.
// console.log(swapCase(12345));
This is a solution that uses regular expressions. It matches each word-char globally, and then performs a function on that matched group.
function swapCase(letters) {
return letters.replace( /\w/g, function(c) {
if (c === c.toLowerCase()) {
return c.toUpperCase();
} else {
return c.toLowerCase();
}
});
}
#this is a program to convert uppercase to lowercase and vise versa and returns the string.
function main(input) {
var i=0;
var string ='';
var arr= [];
while(i<input.length){
string = input.charAt(i);
if(string == string.toUpperCase()){
string = string.toLowerCase();
arr += string;
}else {
string = string.toUpperCase();
arr += string;
}
i++;
}
console.log(arr);
}
Split the string and use the map function to swap the case of letters.
We'll get the array from #1.
Join the array using join function.
`
let str = 'The Quick Brown Fox Jump Over A Crazy Dog'
let swapedStrArray = str.split('').map(a => {
return a === a.toUpperCase() ? a.toLowerCase() : a.toUpperCase()
})
//join the swapedStrArray
swapedStrArray.join('')
console.log('swapedStrArray', swapedStrArray.join(''))
`
A new solution using map
let swappingCases = "So, today we have REALLY good day";
let swapping = swappingCases.split("").map(function(ele){
return ele === ele.toUpperCase()? ele.toLowerCase() : ele.toUpperCase();
}).join("");
console.log(swapping);
As a side note in addition to what has already been said, your original code could work with just some minor modifications: convert the string to an array of 1-character substrings (using split), process this array and convert it back to a string when you're done (using join).
NB: the idea here is to highlight the difference between accessing a character in a string (which can't be modified) and processing an array of substrings (which can be modified). Performance-wise, Fabricator's solution is probably better.
var swapCase = function(str){
var letters = str.split("");
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){
letters[i] = letters[i].toUpperCase();
}else {
letters[i] = letters[i].toLowerCase();
}
}
str = letters.join("");
console.log(str);
}
var text = 'So, today we have REALLY good day';
swapCase(text);

Categories

Resources