admin管理员组文章数量:1426491
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
4 Answers
Reset to default 2The 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.
本文标签: javascriptInvalid lefthand side in assignment expressionStack Overflow
版权声明:本文标题:javascript - Invalid left-hand side in assignment expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465957a2659535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论