Hello,
I am attempting to turn any sentence into a whitespace-free camelCase sentence, but something isn’t as intended.
Please advise
function convertToCamelCase(words) {
const noSpace = words.replace(/ /g, "");
const wordsArr = noSpace.split("");
for (let i = 0; i < wordsArr.length; i++) {
if (i % 2 == 0) {
wordsArr[i].toUpperCase();
} else {
wordsArr[i].toLowerCase();
}
}
return wordsArr;
}
ilenia
September 29, 2022, 12:57pm
#2
what do you think this is doing?
Hi @leedsleedsleedsleeds ,
Just to clarify what you want to do is?
input => "this is a sentence" ===> output => "ThisIsASentence"
Am I correct?
I was coding for too long and my brain bottomed out!
The challenge I am attempting to complete is actually, as you both have gathered, to return camelCaseLikeThis. I am sure I can do this, however…
… I would still like to know how to do the stupid thing I was attempting here which is to make the output lOoKlIkEtHiS, with each character alternating case. I think my for loop is being ignored entirely.
Any ideas? Thanks for your time either way
If i, when divided by 2, has a remainder of 0 change the element at index i of wordsArr to upper case.
This way every other character will be upper case.
This is not doing what you think it is doing.
ugwen
September 29, 2022, 3:11pm
#9
Description
The toLowerCase()
method returns the value of the string converted to lower case. toLowerCase()
does not affect the value of the string str
itself.
from: DevDocs
ilenia
September 29, 2022, 3:16pm
#10
Didn’t you said camelCase? in camelCase the uppercase letters are not every second character
also what the other said about toUpperCase and toLowerCase
Cheers for that and sorry to everyone I initially infected with my own confusion
function toSuperCamel(str) {
const noSpace = str.replace(/ /g, "");
const wordsArr = noSpace.split("");
for (let i = 0; i < wordsArr.length; i++) {
if (i % 2 == 0) {
wordsArr[i] = wordsArr[i].toUpperCase();
} else {
wordsArr[i] = wordsArr[i].toLowerCase();
}
}
const superCamel = wordsArr.toString().replace(/,/g, "");
return superCamel;
}
console.log( toSuperCamel("You cant make a Tomlete without breaking some Gregs") );```
1 Like
This is a bit unnecessary. Read about the join method.
1 Like