Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function get_products_of_all_ints_except_at_index(arr) {
var numbersBeforeIndex = [];
var numbersAfterIndex = [];
for (var i = 0; i < arr.length; i++) {
numbersBeforeIndex[i] = arr.slice(0, i);
if (numbersBeforeIndex[i].length) {
numbersBeforeIndex[i] = numbersBeforeIndex[i].reduce(function (a, b) {
return a * b;
});
}
else {
numbersBeforeIndex[i] = 1;
}
numbersAfterIndex[i] = arr.slice(i + 1, arr.length);
if (numbersAfterIndex[i].length) {
numbersAfterIndex[i] = numbersAfterIndex[i].reduce(function (a, b) {
return a * b;
});
}
else {
numbersAfterIndex[i] = 1;
}
}
var products = [];
for (i = 0; i < arr.length; i++) {
products[i] = numbersBeforeIndex[i] * numbersAfterIndex[i];
}
return products;
}
@adeubank

This comment has been minimized.

Copy link
Owner Author

@adeubank adeubank commented Nov 6, 2014

Work in progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment