0
function neighbor(color_indices) {
    var neighbor_face = [];
    var neighbor_index_temp = [];
    //initialize to the given length 
    var color_indices_length = color_indices.length; 
    for (var i = 0; i < color_indices_length; i++) {    
        if (color_indices[i] % 2 == 0 ) {
            if (color_indices[i] % 10 == 8) {
                neighbor_index_temp[0] = (color_indices[i]) + 1; 
                neighbor_index_temp[1] = (color_indices[i]) - 17;
                neighbor_index_temp[2] = (color_indices[i]) - 19;
                //check if it is in the array
                for (var k = 0; k < 3; k++){
                   if ($.inArray(neighbor_index_temp[k],color_indices) != -1){
                        color_indices.push(neighbor_index_temp[k]);
                    }
                 }

The input : color_indices would be an array of the global variable. I am trying to push neighbor_index_temp of only new to the color_indices. I tried to implement $.inArray but it doesn't seem to work. Any suggestions?

Thanks!

John Doe
  • 62
  • 11
  • It might help if you had the use of a set. Found a great SO [article](http://stackoverflow.com/questions/7958292/mimicking-sets-in-javascript) which discusses how to mimic sets in JavaScript. – Tim Biegeleisen Feb 16 '15 at 01:12
  • It looks like part of a colouring algorithm. Can you explain "color_indices[i] % 2"? What are you encoding in color_indices items? Those each bit have a special purpose? – Dominique Fortin Feb 16 '15 at 03:47
  • @DominiqueFortin Sure - on three.js, I am trying to color the neighboring faces of the selected face. I found out that when the selected face's index is even number and odd number, they have different patterns of the neighboring indexes :) – John Doe Feb 16 '15 at 04:47
  • @JangHeeI Sorry I though you meant a graph colouring algorithm where no two adjacent nodes can have the same color. – Dominique Fortin Feb 16 '15 at 05:05
  • @DominiqueFortin It is no problem. Thanks for trying to help :) – John Doe Feb 16 '15 at 05:18

1 Answers1

0

You can use indexOf to achieve this. Ie.

if(color_indices.indexOf(neighbor_index_temp[k]) == -1) {
    // value neighbor_index_temp[k] is not in color_indices array
}

Edit : $.inArray might do the same as indexOf, in this case you might want to change '!= -1' to '== -1'

G Roy
  • 166
  • 3