# Java & JavaScript Solution, With Extra Space

• 1, Calculate the product before the number
2, Calculate the product after the number
3, One by one calculate the product of the product from step 1 & 2
Java Solution:

``````public class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
//int[] res = new int[n];
int[] res_before = new int[n];
int[] res_after = new int[n];
res_before[0] = 1;
res_after[n - 1] = 1;
for(int i = 1; i < n; i++) {
res_before[i] = res_before[i - 1] * nums[i - 1];
}
for(int i = n - 2; i >= 0; i--) {
res_after[i] = res_after[i + 1] * nums[i + 1];
}
for(int i = 0; i < n; i++) {
res_after[i] = res_before[i] * res_after[i];
}
return res_after;
}
}
``````

JavaScript Solution is the same:

``````/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
var i;
var res_before = [];
var res_after = [];
//var res = [];
res_before[0] = 1;
res_after[nums.length - 1] = 1;
for(i = 1; i < nums.length; i++) {
res_before[i] = res_before[i - 1] * nums[i - 1];
}
for(i = nums.length - 2; i >= 0; i--) {
res_after[i] = res_after[i + 1] * nums[i + 1]
}
for(i = 0; i < nums.length; i++) {
res_after[i] = res_before[i] * res_after[i];
}
return res_after;
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.