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
  • 155,085
  • 27
  • 166
  • 222
Jack_of_All_Trades
  • 10,326
  • 16
  • 55
  • 85

2 Answers2

1

Yes, there are some options:

JavaScript indexOf()

jQuery.inArray()

speti43
  • 2,648
  • 1
  • 17
  • 23
0

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

zozo
  • 7,781
  • 18
  • 72
  • 128