Code Advice - How to make more concise (Javascript/Jquery) -


I'm trying to make my code more intense (i.e., less repeated code). I have got some advice from my supervisor how I can make my recent code more brief, but I am not sure how to do it.

I have some coordinates that I am using to check that a user clicks within a certain area of ​​a div. I was told that I should put all the coordinates in the array and get them through "loop" when they want them. I-think- I understand what he was suggesting, but I can not wrap my head around it because I am still inexperienced with programming. Here's what I have done here, get a better idea of ​​what you are doing for:

  $ ("# div1"). Click (function (e) {// arrays in which x and y fields of a rectangular area around a field // [minx, max, miny, maxy] div1_Coord_Area1 = [565, 747, 310, 423]; div1_Coord_Area2 = [755, 947, 601, 715], if (areaX> = Reg2_Coord_Farm1 [0] & amp; amp; areaX & lt; = Reg2_Coord_Farm1 [1] in & amp; amp; amp; AreaY> = Reg2_Coord_Farm1 [ 2] in & amp; amp; amp; amp; areaY & lt; = Reg2_Coord_Farm1 [3]) {warnings ("Area you first clicked");} if (areaX> Reg2_Coord_Farm2 [0] & amp; AreaX & lt; = Reg2_Coord_Farm2 [1] in & amp; amp; amp; AreaY> = Reg2_Coord_Farm2 [2] in & amp; amp; amp; amp; & amp; amp; amp; & amp; area; Clicked ");}});   

Do not worry about how I calculate; I left that code out of method because it is basically irrelevant, but it is is there if you were thinking.

I created an array for each set coordinate and just call them. This is not a huge array filled with all coordinates of each area, although "Can you think of a way to do this, or can I do the best at this time?

< Div class = "post-text" itemprop = "text">

I think what they meant, something else like this:

  $ ("# div1") Click (function (e) {// arrays in which rectangular area's X and Y values ​​are a form // for each array: [area 1, area 2, area 3, ... Area field] var minex = [565, 755]; var maxX = [747, 947]; var minway = [310, 601]; var maxY = [423, 715]; for (var i = 0; I & lt; MinX.length; i ++) {if (areaX> = mINX [i] & amp; amp; areaX & lt; = Maxx [i] & amp; amp; AreaY & gt; = MINY [i] & Amp; amp; amp; amp; amp; amp; amp; and & amp ;;;;;;;;;;;;;;;;;41); 42; ;;;;;;;;;;;;;2 ;;;;;;;:;;;;;;;;;;;;;;;; r; in the area; + + (i + 1));}}};   

In this way, There may be many more areas for the loop but it does not have to change anytime because it will always run through all the objects in the array. But like 2 or 10, or 100.

Comments