Issues using toUpper/LowerCase

Hello,

I am attempting to turn any sentence into a whitespace-free camelCase sentence, but something isn’t as intended.

Please advise :slight_smile:

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;
}

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?

“thisIsASentence” i fink

I was coding for too long and my brain bottomed out! :sweat_smile:

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 :slight_smile:

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.

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

Didn’t you said camelCase? in camelCase the uppercase letters are not every second character

also what the other said about toUpperCase and toLowerCase

The user mentioned:

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.

Cheers for that and sorry to everyone I initially infected with my own confusion :sweat_smile:

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