Why non-space elements in string aren't changing to upperCase - javascript

I want to write a function to change the characters in a string at even indices to uppercase. I don't want my program to count the spaces as an even index, even if it falls on an even index.
For example: 'This is a test' => 'ThIs Is A TeSt'
I originally had this solution, but I could not get it to work to ignore the space characters when counting the even indices.
function toWeirdCase(string) {
return string.split("").map((x,i) => i%2=== 0 && x!== " " ? x.toUpperCase() : x).join("")
}
This is my second attempt and I don't know why the string elements aren't actually changing to uppercase. Any help on this would be appreciated. It is just returning the original string.
function toWeirdCase(string) {
let indexCount = 0;
let isSpace = false;
for (let i = 0; i < string.length; i++) {
if (string[i] === " ") {
isSpace = true;
}
if (indexCount % 2 === 0 && !isSpace) {
string[indexCount] = string[indexCount].toUpperCase();
}
indexCount++;
isSpace = false;
}
return string;
}

Answer:
You can use a modified reduce function that utilizes a closure as a character counter. This has the benefit of completing the transformation in one pass:
["", ...str].reduce((n =>
(r, c) => r += /\s/.test(c) ? c : c[`to${n++ & 1 ? "Lower" : "Upper"}Case`]())(0)
);
Example:
const biUpperCase = str => ["", ...str].reduce((n =>
(r, c) =>r += /\s/.test(c) ? c : c[`to${n++ & 1 ? "Lower" : "Upper"}Case`]())(0)
);
let test = biUpperCase("This is a Test");
console.log(test);
Explanation:
n is a character counter that keeps track of all non-space characters. You can think of this as an additional index that only worries about non-space characters.
We use this to determine whether or not a character is an even or odd non-space character by performing bitwise AND ( n & 1 ) or, alternatively, by performing a modulus operation ( n % 2 )
r is the accumulator in the Array.prototype.reduce method. This is what is returned by our reduce method.
Since there was no secondary parameter to Array.prototype.reduce, the first index of the Array is used as the accumulator.
This is why we perform ["", ...str] instead of simply [...str].
Syntactically we could also have written [...str].reduce( fn , "" ) instead of ["", ...str].reduce( fn ), but this would alter our succinct code.
c is the current character that we are looking at within the string array. We use RegExp.prototype.match to determine if it's a space character.
if it is we simply add the space to r ( our accumulated string )
if it is not we add a transformed character to r ( our accumulated string )
To determine which case transformation( upper or lower ) should be applied we check if n ( our character counter ) is even or odd.
if n++ & 1 is truthy the case is lower
if n++ & 1 is falsy the case is upper
Aside:
You'll notice in the snippet and code I provided that I changed your parameter name string to str. The reason for this is because String is a built-in Constructor in JavaScript and it's best to never purposefully "cross the streams" when naming variables.
In the current way that you're attempting to use this variable it makes no difference since it's properly scoped, and truthfully it is up to you if you want to take my advice. Just be aware that it could lead to an annoying, invisible problem.
Hope this Helps! Happy Coding!

You could rewind the index counter for a single word.
function toWeirdCase(string) {
return Array
.from(
string,
(i => x => (/[a-z]/i.test(x) ? i++ : (i = 0)) % 2 ? x : x.toUpperCase())
(0)
)
.join('');
}
console.log(toWeirdCase('This is a test')); // 'ThIs Is A TeSt'

A string in javascript is immutable so you will need to do something like :
let test = 'This is a test';
test = toWeirdCase(test); //Here you assign the result
And here is an example solution which ignores spaces in the count
function toWeirdCase(string) {
let count = 0;
return string.split("").map((x) => {
if(x !== " ")
count++;
if(count%2 === 0) return x.toUpperCase();
else return x;
}).join("")
}
let test = 'This is a test';
test = toWeirdCase(test);
console.log(test); //THiS iS a TeSt

Like the comments mention, strings in Javascript are immutable. That being said, you can break down the input string on whitespace, do the transformations, and join back into a string, something like this -
function toWeirdCase(sentence) {
return sentence
.split(' ')
.map(word => word
.split('')
.map((c, i) => i % 2 ? c : c.toUpperCase())
.join('')).join(' ');
}

You could store the number of spaces in a variable in the functions scope.
function toWeirdCase(string) {
let spaceCount = 0;
// Personal preference: I like the reduce fn for this, but a similar thing could be achieved with map
return string.split('').reduce((value, letter, index) => {
if (letter === ' ') spaceCount++;
return value += ((index - spaceCount) % 2)
? letter
: letter.toUpperCase();
},'')
}
This returns the leter if the index ingoring the space count has a remainder when divided by 2.

You can achieve this like so:
const str = "this is a test";
function toWeirdCase(str) {
return str.split(" ").map(word => (
[...word].map((c, i) => i % 2 ?
c.toLowerCase() :
c.toUpperCase())).join("")).join(" ");
}
console.log(toWeirdCase(str));
Updated: to set odd indexes toLowerCase() to handle edge cases like acronyms (ie: currency acronyms; "CA", "USD")
Hope this helps,

Related

How to swap first and last letter case in each word after using reverse() in JavaScript

I wanna try swapping cases for the first and last letters in each word that are already reversed using the reverse function.
Let's say I have a string like this.
I am Firdaus
And I wanna make it just like this.
I ma Suadrif
Here is how the script looks like
let reverseWords = str => {
console.log(`Input: ${str}`)
let words = str.split(" ").map(word => {
let chars = word.split("").reverse();
let result = chars.map((char, index) => {
if (index == chars.length-1 && chars.length != 0 && char == char.toUpperCase() && index != 0) {
return char.toLowerCase();
}
return char;
});
return result.join('');
});
return words.join(' ');
}
console.log(`Output: ${reverseWords("I am Firdaus")}`);
If you run the code above, the result is not like what I expected
The uppercase F successfully becomes lowercase f, but the lowercase s failed to become uppercase S. I am confused about how to swap the s become an uppercase S.
Is there a way and the best way to make the string just like what I expected?
Thanks in advance.
It look like you need to compare against the original string's casing to determine whether the returned character should be upper-cased or lower-cased. Use the index of the (reversed) character you're mapping over to access the original character at the same index in the original word.
const reverseWords = str => str
.split(' ')
.map(word => [...word].reverse()
.map((char, i) => (
word[i].toLowerCase() === word[i]
? char.toLowerCase()
: char.toUpperCase()
))
.join('')
)
.join(' ');
console.log(`Output: ${reverseWords("I am Firdaus")}`);

Can this problem be solved using functional programming only?

The prompt was:
Create a function that takes in a string and returns a "URL version" of the string. This simply involves replacing the spaces with %20.
It asked to solve the problem using recursion and using .replace is not allowed.
Here is my solution but I understand the ouputArray is being mutated. Is there any other way to solve this without a mutation?
let inputString = "hello world I am fine";
let outputArray = [];
let stringToUrl = (inputString, n) => {
inputArray = [...inputString]
if(n < inputArray.length) {
if(inputArray[n] !== " ") {
outputArray.push(inputArray[n])
return stringToUrl(inputArray, n+1)
}
else {
outputArray.push("%20")
return stringToUrl(inputArray, n+1)
}
}
return outputArray.join('');
}
console.log(stringToUrl(inputString, 0))
Yes, you can do this with FP. In keeping with How do I ask and answer homework questions?, I won't reply with code, but with pointers.
If you weren't doing this with FP (but still had to write it yourself rather than using the string replace method, etc.), you'd probably use a loop building up a new string by looping through the original string character by character and either adding the original character to the new string or adding %20 to it.
In FP, loops are often done via recursion, and your instructions are to use recursion, so we'll do that instead.
Your function should handle the first character in the string it's given (either keeping it or replacing it with %20), and if that character is the only character, just return that updated "character;" otherwise, it should return the updated character followed by the result of passing the rest of the string (all but that first character) through your function again. That will work through the entire string via recursion, building up the new string. (No need for arrays, string concatenation and substring should be fine.)
Here I have made some changes to your code. Hope this solves your problem.
I don't have to use the second array but make changes to the original array.
let inputString = "hello world I am fine";
let stringToUrl = (inputString, n) => {
inputArray = [...inputString]
if(n < inputArray.length) {
if(inputArray[n] === " ") {
inputArray[n] = "%20"
return stringToUrl(inputArray, n+1)
}
else {
return stringToUrl(inputArray, n+1)
}
}
return inputArray.join('');
}
console.log(stringToUrl(inputString, 0))
const replace = (char: string) => char === ' ' ? '%20' : char;
const convert = (str: string, cache = ''): string => {
const [head, ...tail] = str;
return head
? convert(
tail.join(''),
cache.concat(replace(head))
)
: cache
}
const result = convert("hello world I am fine") // hello%20world%20I%20am%20fine
Playground
I hope this task is not language agnostic, because JS is not best choise in terms of recursion optimization.
One option to do that could be using a call to stringToUrl and use an inner recursive function making use of default parameters passing the values of the variables as function arguments.
For example using an arrow function, and also passing a function as a parameter that does a check to either add %20 to the array with final characters:
const stringToUrl = str => {
const func = (
s,
r = "",
c = s.charAt(0),
f = () => r += c === ' ' ? '%20' : c
) => s.length ? f() && func(s.substr(1), r) : r
return func(str)
}
console.log(stringToUrl("hello world I am fine"));
Output
hello%20world%20I%20am%20fine
const stringToUrl = str => {
const func = (
s,
r = "",
c = s.charAt(0),
f = () => r += c === ' ' ? '%20' : c
) => s.length ? f() && func(s.substr(1), r) : r
return func(str)
}
[
"",
" ",
" ",
"hello world I am fine"
].forEach(s =>
console.log(`[${s}] --> ${stringToUrl(s)}`)
);

Counting brackets in a string

been working on a school problem, and haven't been able to figure it out. Any help is appreciated!
Write a function named countBrackets that accepts a string
and returns the count of square bracket and curly bracket
characters in the string. That is, it should count occurrences of
these four characters “{ } [ ]”. Use function expression syntax.
var countBrackets = function(s){
let sum = 0
for(let i = ""; i == s ; i++ )
if(i ==="{}[]"){
sum+=i
}
return sum}
console.log(countBrackets("[123],{456},[{}]")) //8
console.log(countBrackets("Test string")) // 0
I'm a little confused on how I'm supposed to get it to count a string I guess.
You can use a global regex (regular expression) matching for this. The regex is between / / followed by the g flag to make it global (otherwise it only returns the first result it finds and stops).
Within the regex, | is the OR operator, so you match for /{|}|[|]/
Since [ and ] have special meaning in regular expressions you need to escape those using a \ so your total regex becomes /{|}|\[|\]/g.
This returns an array of matches, I called the function findBrackets.
To get the number of brackets, the function countBrackets just returns the .length of that array.
const findBrackets = str => str.match(/{|}|\[|\]/g);
const countBrackets = str => findBrackets(str) ? findBrackets(str).length : 0;
console.log(findBrackets('qw[e}rt[y]ui{}o{p'));
console.log(countBrackets('qw[e}rt[y]ui{}o{p'));
console.log(countBrackets('no brackets here'));
Edit: seeing the code you posted, you decided to use a for-loop to loop over your string, which is a totally valid solution.
Note that in my above example
const findBrackets = str => str.match(/{|}|\[|\]/g);
Is basically the same but a newer way of writing functions (with a few nuances)
I could have written:
var findBrackets = function(str) {
return str.match(/{|}|\[|\]/g);
}
instead which would be almost the same.
If you want to loop over a string, you can access a letter in the string by using square bracket notation, so for example
const testString = 'hello';
console.log(testString[1]);
Would output the letter 'e'.
So to use this in a function:
const countBrackets = (str) => {
let counter = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === '[' || str[i] === ']' || str[i] === '{' || str[i] === '}') {
counter++;
}
}
return counter;
}
console.log(countBrackets('qw[e}rt[y]ui{}o{p'));
Here you loop over the string from 0 to < length of the string and check every letter of it, by seeing if str[i] is {, }, [ or ], and if it is you increment a counter. After that's done you return the final count.

How can I find at least 1 uppercase in a string?

In es6 js I have :
good = word => {
word.split('').includes(w => w === w.toUpperCase())
}
console.log(good('Bla'))
How can I return true when finding 1 Uppercase in my string?
You can test the string using a regular expression with a character set of all uppercase letters [A-Z]:
const good = word => /[A-Z]/.test(word);
console.log(good('Bla'));
console.log(good('bla'));
Although there are much simpler ways to do this (the regex in Tushar's comment being one of them), it's possible to fix your attempt to work correctly by:
Removing the braces so that the function actually returns a value.
Using .some(), which takes a function as its argument. .includes() doesn't.
Addding const so you're actually declaring your function.
const good = word => word.split('').some(w => w === w.toUpperCase())
console.log(good('Bla'))
console.log(good('bla'))
You can do this if you want the index of it too.
function findUpperCase(str) {
return str.search(/[A-Z]/);
}
// The string which will go thorough the test
let theString = 'Hello World'
// Function to find out the answer
function stringCheck (receivedString) {
// Removing special character, number, spaces from the string to perform exact output
let stringToTest = receivedString.replace(/[^A-Z]+/ig, "")
// Variable to count: how many uppercase characters are there in that string
let j = 0
// Loop thorough each character of the string to find out if there is any uppercase available
for (i = 0; i < stringToTest.length; i++) {
// Uppercase character check
if (stringToTest.charAt(i) === stringToTest.charAt(i).toUpperCase()) {
console.log('Uppercase found: ' + stringToTest.charAt(i))
j++
}
}
console.log('Number of uppercase character: ' + j)
// Returning the output
if (j >= 1) {
return true
} else {
return false
}
}
// Calling the function
let response = stringCheck(theString)
console.log('The response: ' + response)

How to remove specific character surrounding a string?

I have this string:
var str = "? this is a ? test ?";
Now I want to get this:
var newstr = "this is a ? test";
As you see I want to remove just those ? surrounding (in the beginning and end) that string (not in the middle of string). How can do that using JavaScript?
Here is what I have tried:
var str = "? this is a ? test ?";
var result = str.trim("?");
document.write(result);
So, as you see it doesn't work. Actually I'm a PHP developer and trim() works well in PHP. Now I want to know if I can use trim() to do that in JS.
It should be noted I can do that using regex, but to be honest I hate regex for this kind of jobs. Anyway is there any better solution?
Edit: As this mentioned in the comment, I need to remove both ? and whitespaces which are around the string.
Search for character mask and return the rest without.
This proposal the use of the bitwise not ~ operator for checking.
~ is a bitwise not operator. It is perfect for use with indexOf(), because indexOf returns if found the index 0 ... n and if not -1:
value ~value boolean
-1 => 0 => false
0 => -1 => true
1 => -2 => true
2 => -3 => true
and so on
function trim(s, mask) {
while (~mask.indexOf(s[0])) {
s = s.slice(1);
}
while (~mask.indexOf(s[s.length - 1])) {
s = s.slice(0, -1);
}
return s;
}
console.log(trim('??? this is a ? test ?', '? '));
console.log(trim('abc this is a ? test abc', 'cba '));
Simply use:
let text = '?? something ? really ??'
text = text.replace(/^([?]*)/g, '')
text = text.replace(/([?]*)$/g, '')
console.log(text)
A possible solution would be to use recursive functions to remove the unwanted leading and trailing characters. This doesn't use regular expressions.
function ltrim(char, str) {
if (str.slice(0, char.length) === char) {
return ltrim(char, str.slice(char.length));
} else {
return str;
}
}
function rtrim(char, str) {
if (str.slice(str.length - char.length) === char) {
return rtrim(char, str.slice(0, 0 - char.length));
} else {
return str;
}
}
Of course this is only one of many possible solutions. The function trim would use both ltrim and rtrim.
The reason that char is the first argument and the string that needs to be cleaned the second, is to make it easier to change this into a functional programming style function, like so (ES 2015):
function ltrim(char) {
(str) => {
<body of function>
}
}
// No need to specify str here
function ltrimSpaces = ltrim(' ');
Here is one way to do it which checks for index-out-of-bounds and makes only a single call to substring:
String.prototype.trimChars = function(chars) {
var l = 0;
var r = this.length-1;
while(chars.indexOf(this[l]) >= 0 && l < r) l++;
while(chars.indexOf(this[r]) >= 0 && r >= l) r--;
return this.substring(l, r+1);
};
Example:
var str = "? this is a ? test ?";
str.trimChars(" ?"); // "this is a ? test"
No regex:
uberTrim = s => s.length >= 2 && (s[0] === s[s.length - 1])?
s.slice(1, -1).trim()
: s;
Step-by-step explanation:
Check if the string is at least 2 characters long and if it is surrounded by a specific character;
If it is, then first slice it to remove the surrounding characters then trim it to remove whitespaces;
If not just return it.
In case you're weirded out by that syntax, it's an Arrow Function and a ternary operator.
The parenthesis are superfluous in the ternary by the way.
Example use:
uberTrim(''); // ''
uberTrim(' Plop! '); //'Plop!'
uberTrim('! ...What is Plop?!'); //'...What is Plop?'
Simple approach using Array.indexOf, Array.lastIndexOf and Array.slice functions:
Update: (note: the author has requested to trim the surrounding chars)
function trimChars(str, char){
var str = str.trim();
var checkCharCount = function(side) {
var inner_str = (side == "left")? str : str.split("").reverse().join(""),
count = 0;
for (var i = 0, len = inner_str.length; i < len; i++) {
if (inner_str[i] !== char) {
break;
}
count++;
}
return (side == "left")? count : (-count - 1);
};
if (typeof char === "string"
&& str.indexOf(char) === 0
&& str.lastIndexOf(char, -1) === 0) {
str = str.slice(checkCharCount("left"), checkCharCount("right")).trim();
}
return str;
}
var str = "???? this is a ? test ??????";
console.log(trimChars(str, "?")); // "this is a ? test"
to keep this question up to date using an ES6 approach:
I liked the bitwise method but when readability is a concern too then here's another approach.
function trimByChar(string, character) {
const first = [...string].findIndex(char => char !== character);
const last = [...string].reverse().findIndex(char => char !== character);
return string.substring(first, string.length - last);
}
Using regex
'? this is a ? test ?'.replace(/^[? ]*(.*?)[? ]*$/g, '$1')
You may hate regex but after finding a solution you will feel cool :)
Javascript's trim method only remove whitespaces, and takes no parameters. For a custom trim, you will have to make your own function. Regex would make a quick solution for it, and you can find an implementation of a custom trim on w3schools in case you don't want the trouble of going through the regex creation process. (you'd just have to adjust it to filter ? instead of whitespace
This in one line of code which returns your desire output:
"? this is a ? test ?".slice(1).slice(0,-1).trim();

Categories