简体   繁体   中英

Invalid left-hand side in assignment expression

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

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('');
}

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'));

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM