• Resolved Bakhshi

    (@bakhshi)


    Hello
    I’m going to use the array in this site:
    https://www.geeksforgeeks.org/find-closest-number-array/

    // Java program to find element closet to given target. 
    import java.util.*; 
    import java.lang.*; 
    import java.io.*; 
    
    class FindClosestNumber { 
    	
    	// Returns element closest to target in arr[] 
    	public static int findClosest(int arr[], int target) 
    	{ 
    		int n = arr.length; 
    
    		// Corner cases 
    		if (target <= arr[0]) 
    			return arr[0]; 
    		if (target >= arr[n - 1]) 
    			return arr[n - 1]; 
    
    		// Doing binary search 
    		int i = 0, j = n, mid = 0; 
    		while (i < j) { 
    			mid = (i + j) / 2; 
    
    			if (arr[mid] == target) 
    				return arr[mid]; 
    
    			/* If target is less than array element, 
    			then search in left */
    			if (target < arr[mid]) { 
    		
    				// If target is greater than previous 
    				// to mid, return closest of two 
    				if (mid > 0 && target > arr[mid - 1]) 
    					return getClosest(arr[mid - 1], 
    								arr[mid], target); 
    				
    				/* Repeat for left half */
    				j = mid;			 
    			} 
    
    			// If target is greater than mid 
    			else { 
    				if (mid < n-1 && target < arr[mid + 1]) 
    					return getClosest(arr[mid], 
    						arr[mid + 1], target);				 
    				i = mid + 1; // update i 
    			} 
    		} 
    
    		// Only single element left after search 
    		return arr[mid]; 
    	} 
    
    	// Method to compare which one is the more close 
    	// We find the closest by taking the difference 
    	// between the target and both values. It assumes 
    	// that val2 is greater than val1 and target lies 
    	// between these two. 
    	public static int getClosest(int val1, int val2, 
    										int target) 
    	{ 
    		if (target - val1 >= val2 - target) 
    			return val2;		 
    		else
    			return val1;		 
    	} 
    
    	// Driver code 
    	public static void main(String[] args) 
    	{ 
    		int arr[] = { 1, 2, 4, 5, 6, 6, 8, 9 }; 
    		int target = 11; 
    		System.out.println(findClosest(arr, target)); 
    	} 
    } 
    Output:
    9

    I completely replaced the code in the Calculated Field
    But output 9 didn’t show!

    What parts of this array are not known for the plugin?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @bakhshi

    Java and JavaScript are two different programming languages, the programming language supported by browsers is javascript, and you are trying to use a block of Java code in the equations associated to the calculated fields, I’m sorry, but that’s not possible.

    Best regards.

    Thread Starter Bakhshi

    (@bakhshi)

    thank you

                function closest (num, arr) {
                    var curr = arr[0];
                    var diff = Math.abs (num - curr);
                    for (var val = 0; val < arr.length; val++) {
                        var newdiff = Math.abs (num - arr[val]);
                        if (newdiff < diff) {
                            diff = newdiff;
                            curr = arr[val];
                        }
                    }
                    return curr;
                }
                array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];
                number = 150;
                alert (closest (number, array));

    Help me please
    I replaced the word “return” instead of the word “alert”
    But the result does not show!

    alert (closest (number, array));
    return (closest (number, array));

    Plugin Author codepeople

    (@codepeople)

    Hello @bakhshi

    In this case you need to implement your equation using the function structure.

    Insert a calculated field in the form, and enter through its attribute: “Set equation”, the following piece of code:

    
    (function(){
    	function closest (num, arr) {
    		var curr = arr[0];
    		var diff = Math.abs (num - curr);
    		for (var val = 0; val < arr.length; val++) {
    			var newdiff = Math.abs (num - arr[val]);
    			if (newdiff < diff) {
    				diff = newdiff;
    				curr = arr[val];
    			}
    		}
    		return curr;
    	}
        var _array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
    		_number = 150;
        
    	return closest (_number, _array);
    })()
    

    and that’s all.

    Best regards.

    Thread Starter Bakhshi

    (@bakhshi)

    thank you very much indeed!

    Thread Starter Bakhshi

    (@bakhshi)

    You may help me again!
    this is find closest index of array:

    function closest(num, arr) {
        var curr = arr[0],
            diff = Math.abs(num - curr),
            index = 0;
    
        for (var val = 0; val < arr.length; val++) {
            let newdiff = Math.abs(num - arr[val]);
            if (newdiff < diff) {
                diff = newdiff;
                curr = arr[val];
                index = val;
            }
        }
        return index;
    }
    
    var array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
        number = 155;
    
    console.log(closest(number, array));

    I put in the calculated field as follows, But it does not produce results!

    (function(){
    function closest(num, arr) {
        var curr = arr[0],
            diff = Math.abs(num - curr),
            index = 0;
    
        for (var val = 0; val < arr.length; val++) {
            let newdiff = Math.abs(num - arr[val]);
            if (newdiff < diff) {
                diff = newdiff;
                curr = arr[val];
                index = val;
            }
        }
        return index;
    }
    
    var array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362],
        number = 155;
    
    console.log(closest(number, array));
    })()

    thank you.

    Plugin Author codepeople

    (@codepeople)

    Hello @bakhshi

    The line of code:

    
    console.log(closest(number, array));
    

    must be edited as follows:

    
    return closest(number, array);
    

    If you need additional help implementing your specific project, you can contact me through the following link: Customization

    Best regards.

    Thread Starter Bakhshi

    (@bakhshi)

    thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help me about array Find’ is closed to new replies.