1

New person here working on a toy problem building a function that converts a string to camelcase anywhere there is a dash or an underscore. I have almost everything worked out except the second to last line of the function where I am trying to change the characters at each index (from my index array) to uppercase before I return the string. The error I am getting is bad assignment from the left side, but I'm not sure why. I've console logged both sides of the assignment and they seem to be doing what I want, but the assignment itself isn't working. Thank you for any help!

Here is the code:

function toCamelCase(str){
  var stringArray = str.split('');
  var indexArray = [];
  stringArray.forEach(character => {
    if (character === '-' || character === '_') {
      var index = str.indexOf(character);
      str = str.slice(0, index) + str.slice(index+1)
      indexArray.push(index);
    }
    return character;
  })
  indexArray.forEach(index => {stringArray.splice(index, 1)});
  string = stringArray.join('');
  indexArray.forEach(index => {string.charAt(index) = string.charAt(index).toUpperCase()});
  return string;
}
mateotherock
  • 235
  • 5
  • 16
  • Possible duplicate of [Why does chartAt() cause an "Invalid left-hand side in assignment" error in JavaScript?](https://stackoverflow.com/questions/35490593/why-does-chartat-cause-an-invalid-left-hand-side-in-assignment-error-in-java) – Guilherme Lemmi Apr 27 '18 at 22:50

4 Answers4

2

The problem is with using string.charAt() on the left hand side. That is not possible as you're trying to assign something to the result of a function, all in the same call. Store the value of string.charAt() in an intermediary variable and it should work. Check the code below for a working example, using a slightly different approach:

function toCamelCase(str){
  var stringArray = str.split('');
  var indexArray = [];
  stringArray.forEach(character => {
    if (character === '-' || character === '_') {
      var index = str.indexOf(character);
      str = str.slice(0, index) + str.slice(index+1)
      indexArray.push(index);
    }
    return character;
  });
  indexArray.forEach(index => {stringArray.splice(index, 1)});
  return stringArray.map((char, index) => {
    return indexArray.includes(index) ? char.toUpperCase() : char;
  }).join('');
}
Guilherme Lemmi
  • 3,271
  • 7
  • 30
  • 30
1

For taking an approach by iterating the characters, you could use a flag for the following upper case letter.

function toCamelCase(str) {
    var upper = false;

    return str
        .split('')
        .map(c => {
            if (c === '-' || c === '_') {
                upper = true;
                return '';
            }
            if (upper) {
                upper = false;
                return c.toUpperCase();
            }
            return c;
        })
        .join('');
}

console.log(toCamelCase('foo----bar_baz'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Ah thank you both for pointing me in the right direction. Instead of joining it back to a string I took advantage of it being an array already and just looped through that first.

This code worked...

function toCamelCase(str){
  var stringArray = str.split('');
  var indexArray = [];
  stringArray.forEach(character => {
    if (character === '-' || character === '_') {
      var index = str.indexOf(character);
      str = str.slice(0, index) + str.slice(index+1)
      indexArray.push(index);
    }
    return character;
  })
  indexArray.forEach(index => {stringArray.splice(index, 1)});
  indexArray.forEach(index => {stringArray[index] = stringArray[index].toUpperCase()});
  var string = stringArray.join('');
  return string;
}
mateotherock
  • 235
  • 5
  • 16
0

As strange as it sounds what fixed this error was to add ; semicolon at the end of line where the Parsing error: Invalid left-hand side in assignment expression occurred. More context here.

Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124