Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Integer value and IN operator
Message
From
13/03/2016 08:13:42
 
 
To
13/03/2016 07:42:27
General information
Forum:
Javascript
Category:
Other
Miscellaneous
Thread ID:
01632871
Message ID:
01632958
Views:
47
I hope not! ;-)

Since the array is sorted, if there are any duplicates in the properties they are stored contiguously. The function tests x against y, and y against z, but if z is the last element it does need to be compared to anything else. Therefore, the last element (in test order) is never tested.

The test is made backwards because it can stop when it finds the first 0 (which is a bit better than to skip all zeros frontend to start testing).

>isn't there a problem with first element of sorted array?
>
>>And a bit clearer, now, by adding a test function to the object (or to its class):
>>
>>
>>item.hasDuplicateCenters = function(){
>>	var hold = [this.prCtr1, this.prCtr2, this.prCtr3, this.prCtr4, this.prCtr5, this.prCtr6];
>>	hold.sort();
>>	for (var index = 5; index > 0; index--){
>>		if (hold[index] != 0){
>>			if (hold[index] === hold[index - 1]) return true;
>>		} else return false;
>>	}
>>
>>	return false;
>>}
>>
>>
>>if (item.hasDuplicateCenters())
>>	errorMsg = resourceFactory.getResource('Messages', 'allDifferentProfitCenters')
>>
>>
>>>Hi Antonio,
>>>
>>>I am not exactly sure about your answer. Here is my current method implementation - I want to make sure that all selected profit centers are different (e.g. I didn't select the same center twice in case I selected it)
>>>
>>>
>>>if ((item.prCtr1!=0 && (item.prCtr1 == item.prCtr2 || item.prCtr1== item.prCtr3 
>>>                         || item.prCtr1 == item.prCtr4 || item.prCtr1 == item.prCtr5 || item.prCtr1 == item.prCtr6))
>>>                         || 
>>>                         (item.prCtr2!=0 && (item.prCtr1 == item.prCtr2 || item.prCtr2== item.prCtr3 
>>>                         || item.prCtr2 == item.prCtr4 || item.prCtr2 == item.prCtr5 || item.prCtr2 == item.prCtr6))
>>>                         ||
>>>                         (item.prCtr3!=0 && (item.prCtr3 == item.prCtr1 || item.prCtr3== item.prCtr2 
>>>                         || item.prCtr3 == item.prCtr4 || item.prCtr3 == item.prCtr5 || item.prCtr3 == item.prCtr6))
>>>                         || 
>>>                         (item.prCtr4!=0 && (item.prCtr4 == item.prCtr1 || item.prCtr4== item.prCtr2 
>>>                         || item.prCtr4 == item.prCtr3 || item.prCtr4 == item.prCtr5 || item.prCtr4 == item.prCtr6))
>>>                         ||
>>>                         (item.prCtr5!=0 && (item.prCtr5 == item.prCtr1 || item.prCtr5== item.prCtr2 
>>>                         || item.prCtr5 == item.prCtr3 || item.prCtr5 == item.prCtr4 || item.prCtr5 == item.prCtr6))
>>>                         ||
>>>                         (item.prCtr6!=0 && (item.prCtr6 == item.prCtr1 || item.prCtr6== item.prCtr2 
>>>                         || item.prCtr6 == item.prCtr3 || item.prCtr6 == item.prCtr4 || item.prCtr6 == item.prCtr5)))
>>>
>>>                     {
>>>                         errorMsg = resourceFactory.getResource('Messages', 'allDifferentProfitCenters')
>>>                     }
>>>
>>>>
>>>>parseFloat([0,item.prCtr2, item.prCtr3, item.prCtr4, item.prCtr5, item.prCtr6].join(""))
>>>>
>>>>
>>>>will be 0 if no other profit center but item.prCtr1 is specified.
>>>>
>>>>>Hi everybody,
>>>>>
>>>>>I found this page
>>>>>
>>>>>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
>>>>>
>>>>>I am writing a function for checking profit centers. An item can have 6 profit centers. The values are saved as item.prCtr1, item.prCtr2, etc.
>>>>>
>>>>>I want to make sure that if they are specified (e.g. not 0/null), then they are all different.
>>>>>
>>>>>So, I am going to write like this
>>>>>
>>>>>
>>>>>if (item.prCtr1!=0 && item.prCtr1 in new Array(item.prCtr2, item.prCtr3, item.prCtr4, item.prCtr5, item.prCtr6) )
>>>>>
>>>>>The problem here that according to that reference, it will also compare with the array's index and so I may get false positives.
>>>>>
>>>>>Do you see a way to write the above code in a simple manner? Or I'd rather write 5 OR checks for all 6 of them?
>>>>>
>>>>>What do you think?
>>>>>
>>>>>Thanks in advance.
----------------------------------
António Tavares Lopes
Previous
Reply
Map
View

Click here to load this message in the networking platform