Why not replace it?

var output = '565^23';
for(var i=0, l=output.length; i<l; i++){ if(output[i]="='^'" && i>0 && i<l-1){ output[i]="Math.pow(" +output[i-1]+','+output[i+1]+')'; output[i-1] ; if(i>=l-2 || output[i+2]!=='^'){
output[i+1]=";
}

i++;
}}
alert(output);</l-1){></l>


Why can't I assign new values to output[i] ?
June 5th 19 at 21:42
2 answers
June 5th 19 at 21:44
Strings in js are immutable index. It is better to collect a new line.
June 5th 19 at 21:46
So it will work.
var output = '565^23'.split(");
for (var i = 0, l = output.length; i < l; i++) {
 if (output[i] == '^' && i > 0 && i < l - 1) {
 output[i] = 'Math.pow(' + output[i - 1] + ',' + output[i + 1] + ')';

 output[i - 1] = ";

 if (i >= l - 2 || output[i + 2] !== '^') {
 output[i + 1] = ";
}

i++;
}
}
alert(output.join("));
Thank you! - kiarra44 commented on June 5th 19 at 21:49

Find more questions by tags JavaScript