Assigning variable to value of a function (Invalid left-hand side in assignment Error)


#1

I started created a simple calculator using javascript that was able to calculate values with basic operators and decimals and everything was working well. I then decided I wanted to expand the calculator so I created a new design for the calculator that included more advanced capabilities and I changed around the variable and function names to make them more easy to understand and now I'm getting an Uncaught ReferenceError: (line labeled below)

var equal = document.getElementById('equals');
    equal.addEventListener('click', function () {
		 
        var equalSign = document.getElementById('equalSign');
var fixedExpress="";
        if (isValidExpress() === true) {
		fixedExpress = calcExpress();
			
			resNum = eval(fixedExpress); //getting error from this line
			
            

        } else {
            resNum = "Not Valid Expression";
        }
        expressOut.firstChild.nodeValue = expressStr + equalSign.firstChild.nodeValue;
        resOut.firstChild.nodeValue = resNum;

        isDoneEval = true;


    });

I've been going back and forth between my old and new code and I can't figure out why they look similar but aren't evaluating the same. I've looked up some ideas and I've seen a couple places that suggest that you can't assign a variable to the value of a function. But if that's the case, why did it work the first time and how do I fix the problem now?

I've also looked at the above function in the console step by step, with dummy values and it looks like everything is working correctly.

Here's a link to the most recent calculator project I've been working on:

Here's a link to the original calculator that is working how I expect:

Thanks in advance!


#2

What does fixedExpress contain when it's evaluated?


#3

When each button is clicked the value gets added to expressStr. ExpressStr holds the whole expression. CalcExpress() takes expressstr and convers special characters to their actual value so the divide special character will get conveyed to / which allows expressStr to get evaluated using eval


#4

That didn't answer the question that I asked you.


#5

Your absolutely right. My apologies. FixedExpress returns the current expression (a string) with the special characters replaced with their corresponding operator.


#6

I ask you what fixedExpress contains, because that is where your problem is occurring.

With 2+3 for example, what string does fixedExpress contain when the eval statement occurs?


#7

fixedExpress would contain (β€˜2+3’)


#8

That's what it should contain, but currently it doesn’t. You need to investigate what's actually contained in the fixedExpress string value.


#9

I figured it out! FixedExpress was containing (β€˜2+3=β€˜). The = sign was throwing everything off. Thank you for guiding me.


#10

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.