Javascript Clusterfck Metric
===

So I am converting an old data visualization to a new platform and I am a little bit stuck on their community sorting feature. In the original code, it looks like the author uses agglomerative clustering with a cosine similarity calculator. I figured the best way to approach this in Javascript would be to make a tree with clusterfck, using my custom cosine similarity function as the metric. The tree sorts ALMOST correctly for each set of data I pass. (But due to project specifications, "almost" isn't good enough). I checked my algorithm and everything looks right, but when I compare my results using cosine similariy and euclidean distance, I get the same sorting result. 

What could be causing this? I think I may be passing something incorrectly and clusterfck is passing euclidean as a default. Below is a chunk of my code. Can someone verify? (Also, is there an easier way to calculate cosine similarity? I don't think JS has a built in dot product).


    clusters = clusterfck.hcluster(relationArray, clusterfck.cosSim2, clusterfck.SINGLE_LINKAGE);
    postOrder(clusters);
    function postOrder(t) {
	i++;
	if (t == null) {
		return;
	} else {
		postOrder(t.left);
		postOrder(t.right);
		if (t.left == null && t.right == null) {
			communityArr.push(t.canonical[0]);
		} else {
			return;
		}
	}
    }

    function cosSim2(arr1, arr2) {
	var d1 = 0,
		d2 = 0,
		cos = 0;
	for(var i = 0; i < arr1.length; i++) {
		d1 += Math.pow(arr1[i], 2);
	}

	for(var j = 0; j < arr2.length; j++) {
		d2 += Math.pow(arr2[j], 2);
	}

	d1 = Math.sqrt(d1);
	d2 = Math.sqrt(d2);

	for(var j = 0; j < arr2.length; j++) {
		if (arr1[j] == null) {
			cos += 0;
		} else {
			cos += arr1[j] * arr2[j];
		}
	}
	var cosSimilarity = cos / (d1 * d2);
	return cosSimilarity;
    }