0

Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:

var arr=[1,3,4,'+','-', or whatever]

function value_check(user_click){

  var operators=['+','-','/','*','.']
  for (var i=0;i<operators.length;i++){
    if (arr[arr.length-1]==operators[i]){var value1='operator found';}
    if (user_click==operators[i]){
     var value2= value1;alert("consecutive operators"); break;
    }
  }
}

I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:

if (arr[arr.length-1] && user_click BOTH ARE IN operators array) 
  alert("consecutive operators)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88

2 Answers2

1

Yes, there are some options:

JavaScript indexOf()

jQuery.inArray()

speti43
  • 2,886
  • 1
  • 20
  • 23
0

arrayName.indexOf() is what you are looking for.

zozo
  • 8,230
  • 19
  • 79
  • 134