aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes how to compare values in an array if they are the same or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to compare values in an array if they are the same or not" Watch "how to compare values in an array if they are the same or not" New topic
Author

how to compare values in an array if they are the same or not

Amer Ramamni
Greenhorn

Joined: Nov 09, 2012
Posts: 2
Hi All,

I need help in java script, I thought I should give this forum a shot.
I have an array that can contains the following examples:

Examples:
array = {"Yes", null, null, "Yes"} - return "Yes"
array = {"Yes", "Yes", "Yes", "Yes"} - return "Yes"
array = {"Yes", "No", "Yes", "Yes"} - return "Rej" because they don't match
array = {"No", "No", null, null} - return "No"
array = {null, null, null, null} - return null
array = {"No", "No", "No", "No"} - return "No"
array = {"Yes", "No", null, null} - return "Rej"

Anytime the array has a different value other than null, then the return has to be "Rej".

I have started working on this, however its not working for all cases. I do need help.

Here is my code:




Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

Sorry, not really grokking what you are trying to accomplish.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Amer Ramamni
Greenhorn

Joined: Nov 09, 2012
Posts: 2
really .....
Anytime the array has a different value other than null, then the return has to be "Rej", otherwise its one of the values in the array.
Please look at my examples.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Let me take a wild guess at what those examples mean.

(1) Remove all the null entries from the array.

(1.5) If there aren't any more entries, return null.

(2) If all of the remaining entries are the same, return one of them.

(3) Otherwise, return "Rej".
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Create a temporary Set.

Iterate through the Array, and add each Element to the Set (ignore nulls).

Since Sets don't allow duplicates, if the size is 1 then return the first(only) element, e.g: "Yes" or "No".
If the set is empty, size=0, return null
If the set size > 1 then return "Rej", since you would found a "Yes" and a "No".

WP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

JavaScript has no Set type.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

OOPS!

Misread the OP.

My bad.. thumbs up anyway!

WP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to compare values in an array if they are the same or not