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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply 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: 56192
    
  13

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


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 16483
    
    2

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: 860

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: 56192
    
  13

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

Joined: Mar 28, 2012
Posts: 860

OOPS!

Misread the OP.

My bad.. thumbs up anyway!

WP
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to compare values in an array if they are the same or not
 
Similar Threads
Drawing lines between JPanels/JInternalFrames
Struts 2 and auto population of HTML tables
How to add a merge array from 2 others arrays and print it
Finding Nth Largest element of an array without sorting
Can anybody help??