Question: I have a web page for editing a **product** and the UI has **different behavior** depending on **product** type.

**It consist in minor changes like**:
 A specific field is mandatory just for one product type, a dropdown list has fewer options for 2 product types, an `ajax` call is made to a different URL, etc.

1/ How can I write an "elegant" code in JavaScript, so I would like to respect open-closed principle and to avoid conditions like:

```
var idType = $('#idtype').val();
if (idType == 3)

```

...

2/ Is there a way to use `"enums"` in JavaScript?

3/ Is there a design-pattern that refers to this situation? The closest I know is `Strategy` pattern.

4/ In my case, those changes can be handled in `JavaScript` code. But how should **I model** the case when behavior is different on **client-side and server-side**?

Answer: 
You structure declaration:

```
    typedef struct {
    char *word;
    int occurences;
    } HashItem;

```

declares a typedef name for an anonymous struct. There is a `HashItem` type that's a structure, but there is no `struct HashItem` type.

So when your `compare_func()` has the following declarations:

```
const struct HashItem* aa = (HashItem*)a;
const struct HashItem* bb = (HashItem*)b;

```

those `struct HashItem*` variables are pointers to a forward declared `struct HashItem` that has nothign to do with the HashItem strucuture above.

Just change those variable declarations to:

```
const HashItem* aa = (HashItem*)a;
const HashItem* bb = (HashItem*)b;

```

and/or change the declaration of the structure to:

```
    typedef struct HashItem {
    char *word;
    int occurences;
    } HashItem;

```

However, there's another issue (as mentioned in other answers): you are apparently sorting an array of pointers to `HashItem` objects, but your `compare_function()` is being written as if you're sorting an array of the objects (not pointers).

To address this:

```
int compare_func(const void *a, const void *b)
{
    // get HashItem*'s from the HashItem**'s
    const HashItem* aa = *((HashItem**)a);
    const HashItem* bb = *((HashItem**)b);

    int val_1 = aa->occurencies;
    int val_2 = bb->occurencies;

    if (val_1 == val_2) {
        return 0;
    } else if (val_1 > val_2) {
        return 1;
    } else {
        return -1;
    }
}

```

Finally (for now anyway), this function is returning the address to a local array, so the data it points to is no longer valid:

```
HashItem **array_getarray(DynArray *array){
    HashItem **toreturn[array->number_of_values];
    int i;
    for(i = 0; i < array->number_of_values; i++){
        toreturn[i] = array_get(array, i);
    }
    return toreturn;
}

```

I think you'll need to allocate the array you're retuning using `malloc()` or `calloc()` or something. But what I really think you need to do is step back and create some drawing of your data structures and think about the lifetime of the various objects contained in them and how those lifetimes can be tracked an managed so that you don't have leaks, double frees, or pointer dereferences to no longer valid objects.
Change `qsort(standard_array, n, sizeof(HashItem), compare_func);` to

```
qsort(standard_array, n, sizeof(HashItem*), compare_func);

```

In function `void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));`

the third parameter `size_t size` stands for:

> 
> Size in bytes of each element in the array.
> 
> 
>