最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Invalid left-hand side in assignment expression - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Apr 27, 2018 at 20:50 mateotherockmateotherock 2975 silver badges19 bronze badges 1
  • Possible duplicate of Why does chartAt() cause an "Invalid left-hand side in assignment" error in JavaScript? – Guilherme Lemmi Commented Apr 27, 2018 at 22:50
Add a ment  | 

4 Answers 4

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

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论