```
var productExceptSelf = function ( nums ) {
var len = nums.length,
res = [],
preProduct = nums[0],
lastProduct = nums[len - 1];
for ( var n = 0; n < len; n++ ) {
res[n] = 1;
}
for ( var i = 1; i < len; i++ ) {
res[i] *= preProduct;
res[len - i - 1] *= lastProduct;
preProduct *= nums[i];
lastProduct *= nums[len - i - 1];
}
return res;
};
```