2

I have JavaScript calculator wherein I have defined two arrays as follows:

var degInc, degArr = []; 
var radInc, radArr = [];
var PI = Math.PI;
var radStart = (-91*PI/2), radEnd = (91*PI/2);

 for (degInc = -8190; degInc <= 8190; degInc+=180) {
       degArr.push(degInc);
    }
 for (radInc = radStart; radInc <= radEnd; radInc+=PI) {
        var radIncFixed = radInc.toFixed(8);
       radArr.push(radIncFixed);
    }

to be used in conjunction with the tangent function (below) so as to display a value of Undefined in an input (HTML below) should the user attempt to take the tangent of these values (I have included other relavent function as well):

Input -

<INPUT NAME="display" ID="disp" VALUE="0" SIZE="28" MAXLENGTH="25"/>

Functions -

function tan(form) {
  form.display.value = trigPrecision(Math.tan(form.display.value));
}

function tanDeg(form) {
  form.display.value = trigPrecision(Math.tan(radians(form)));
}

function radians(form) {
  return form.display.value * Math.PI / 180;
}

with jQuery -

$("#button-tan").click(function(){
        if (checkNum(this.form.display.value)) {
            if($("#button-mode").val() === 'DEG'){
                tan(this.form); // INSERT OTHER 'if' STATEMENT HERE FOR RAD ARRAY
            } 
            else{
                tanDeg(this.form); // INSERT OTHER 'if' STATEMENT HERE FOR DEG ARRAY
            }

        }
});

I would like to incorporate an array check within the .click function such that if the user input is contained in the array (degArr or radArr depending on the mode), the calculator returns Undefined. Now, I know how to display Undefined in the input display ($('#disp').val('Undefined')), but I cannot figure out how to configure an if statement that checks the relevant array. Is there a way to do so within the #button-tan function where I have commented?

1
  • Can create stacksnippets to demonstrate ? Commented Jan 30, 2016 at 1:46

3 Answers 3

1

Loop through the arrays on click and set a variable if you find a matched value.

You can do something like this:

$("#button-tan").click(function(e) {
e.preventDefault();
var userInput = $('#disp').val();
var buttonMode = $('#button-mode').val();
var displayVal = '';
if (buttonMode === 'DEG') {
    var radFound = false;
    radArr.forEach(function(item) { // changed from degArr
        if (item === userInput) {
            radFound = true;
        }

        if (radFound) {
            displayVal = 'undefined';
        } else {
            tan(this.form);
        }
    });
} else {
    var degFound = false;
    degArr.forEach(function(item) {
        if (item === userInput) {
            degFound = true;
        }

        if (degFound) {
            displayVal = 'undefined';
        } else {
            tanDeg(this.form);
        }
    });
}

});

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't seem to work! The tan button does nothing now
There was a typo with the buttonMode var that caused it to error out, also you need to add a e.preventDefault(), to stop the button from taking the default action.
Her you can see a more refined version of this code mostly working, you will need to fine turn it to you needs but the logic is sound: jsbin.com/kecuba/edit?html,js,console,output You need to be careful though as one array contains strings and the other contains floats.
0

You could create a simple object of a Calculator class, which keeps a reference to these arrays, and use like this. I changed some methods to receive the input as parameter rather than form.

$(function () {
  function Calculator()
  {
    var degInc; 
    this.degArr = []; 
    var radInc; 
    this.radArr = [];
    var PI = Math.PI;
    var radStart = (-91*PI/2);
    var radEnd = (91*PI/2);

     for (degInc = -8190; degInc <= 8190; degInc+=180) {
           this.degArr.push(degInc);
        }
     for (radInc = radStart; radInc <= radEnd; radInc+=PI) {
            var radIncFixed = radInc.toFixed(8);
           this.radArr.push(radIncFixed);
        }
  }

  var calc = new Calculator();

  function tan(input) {
    alert("tan called");
    var value = Math.tan(input.value);
    alert("tan called. value: " + value);
    input.value = value;
  }

  function tanDeg(input) {
    alert("tanDeg called");
    var value = Math.tan(radians(input));
    alert("tanDeg called. value: " + value);
    input.value = value;
  }

  function radians(input) {
    alert("radians called");
    var value = input.value * Math.PI / 180;
    alert("radians called. value: " + value);
    return value;
  }

  $("#button-tan").click(function(){

    alert (calc.degArr);
    alert (calc.radArr);

    var displayInput = $("#disp");

    alert("user input: " + displayInput.val());

    if (!isNaN(displayInput.val()))
    {
        if($("#button-mode").val() === 'DEG')
        {
          if (calc.radArr.indexOf(displayInput.val()) > -1)
          {
            alert("user input is in radArr");
          }
          else
          {
            alert("user input IS NOT in radArr");
            tan(displayInput);
          }
        } 
        else
        {
          if (calc.degArr.indexOf(displayInput.val()) > -1)
          {
            alert("user input is in degArr");
          }
          else {
            alert("user input IS NOT in degArr");
            tan(displayInput);
          }
        }
      }
      else
        alert("Not a number in input");
  });
});

If you wanna do some tests, I created a JSFiddle demo here. Type -8190 in the first input, then click the button. It's gonna be inside the array. Then try typing "DEG" in the second input and clicking again, you'll notice code will check against another array (due to IFs). I couldn't make your auxiliar functions to calculate a value, but I think this helps you with your initial problem.

Comments

0

indexOf should work...

$("#button-tan").click(function(){
    if (checkNum(this.form.display.value)) {
        if($("#button-mode").val() === 'DEG'){
            if (radArr.indexOf(Number(this.form)) > -1) {
                $('#disp').val('Undefined');
            } else {
                tan(this.form);
            }
        } 
        else{
            if (degArr.indexOf(Number(this.form)) > -1) {
                $('#disp').val('Undefined');
            } else {
                tanDeg(this.form);
            }
        }

    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.