Alice Rosier Alice Rosier - 1 year ago 49
Javascript Question

js number function for calculator error

Can anyone explain to me why I get the error message

Uncaught ReferenceError: Invalid left-hand side in assignment
when I run the below function.

function number(a){
var last = parseInt(stream.charAt(stream.length-1));
if(stream === ''){
stream = a;
else if(isNumber(last)){
stream.charAt(stream.length-1) = last*10 + a;
stream += ' '+a;

document.getElementById('display').innerHTML = stream;

Answer Source

The error is in this line:

stream.charAt(stream.length-1) = last*10 + a;

You can't assign something to stream.charAt(). That function only returns a character.

From what I can gather, you're getting the last character from the stream. If it's a integer, you multiply it by 10, then append a to the stream.

Instead of that, this will give the same result:

stream += '0' + a;

Since you're adding the value back into the array, it really doesn't matter if you multiply a single digit integer with 10, or if you just add a "0" after it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download