0

I am trying to build a calculator using JavaScript without eval. I'm trying to concatenate a variable to store the numbers. Is this the correct way of doing so? Also, when ever I add to the variable it is preceded with undefined.

var calculator = {
    run: function(clicked){
        num1: "";
        num2: "";
        numCheck: false;

        switch(clicked){
            case "1":
                document.getElementById("dis").innerHTML += clicked;
                if(this.numCheck===false){
                    if(this.num1==="undefined"){
                        this.num1 = " ";
                        this.num1 = this.num1+"1";
                    }else{
                        this.num1 = this.num1+"1";
                    }
                }else{
                    calculator.run.num2 += "1";
                };

Am I doing this correctly?

hudsond7
  • 666
  • 8
  • 25
  • [Are Variable Operators Possible?](http://stackoverflow.com/questions/5834318/are-variable-operators-possible) – Ram Oct 05 '14 at 02:08
  • How does that relate to what I am asking? – hudsond7 Oct 05 '14 at 02:15
  • "build a calculator using JavaScript **without eval**". You can use object's keys as Math operators. If you think it's not related to your question/problem, ignore it ;) – Ram Oct 05 '14 at 02:17
  • Didn't mean to be rude then, I think my question wasn't clear. I am wondering why when I perform +=1 on a variable the result is "undefined1" instead of 1. – hudsond7 Oct 05 '14 at 02:19
  • This looks like string operations and not arithmetical – Paul S. Oct 05 '14 at 02:40
  • Then what kind of calculator is that? – Ram Oct 05 '14 at 05:13

1 Answers1

0

A few comments:

Missing this on declaration and wrong format on declaration you are using : instead of = inside run class/function the correct should be:

var calculator = {
    run: function(clicked){
        this.num1= "";
        this.num2= "";
        this.numCheck= false;

case missing break and { like:

 switch(clicked){
            case "1":{
              //... your code.
              break; 
             }

Inside your function while using properties that belongs to the class those should be accessed using this instead of using the var calculator on:

calculator.run.num2 += "1"; 

Correct should be

this.run.num2 += "1";

And this can be converted into a single like:

this.num1 = " ";
this.num1 = this.num1+"1";

Like:

this.num1 = " 1";
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • The problem I am having is that when doing so the variable then becomes "undefined1" instead of "1". Thank you for the help with the rest of the code. Why would I need to define it as a var or this within an object? Isn't that a given? – hudsond7 Oct 05 '14 at 02:13
  • 'when doing so...'. What do you mean by that? – Dalorzo Oct 05 '14 at 02:16
  • Sorry, when performing +=1 to the variable it becomes "undefined1" instead of 1. – hudsond7 Oct 05 '14 at 02:17
  • @hudsond7 `num1: "";` is not a valid way to declare the variable therefore `this.num1 += "1"` is actually undefined. This is included in my post above. – Dalorzo Oct 05 '14 at 02:19