• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Amer Ramamni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, not really grokking what you are trying to accomplish.
 
Amer Ramamni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21114
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript has no Set type.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOPS!

Misread the OP.

My bad.. thumbs up anyway!

WP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic