Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save designviacode/7dfa59fc85c18e46dc1924627aa0a148 to your computer and use it in GitHub Desktop.
Save designviacode/7dfa59fc85c18e46dc1924627aa0a148 to your computer and use it in GitHub Desktop.
Product of all array integers except at Index
<script id="jsbin-source-javascript" type="text/javascript">
/* jshint esnext: true */
let integerArr = [1, 7, 3, 4];
const get_products_of_all_ints_except_at_index = (arr) => {
let result = [];
arr.forEach((crr1, index1) => {
let newVal = 1;
arr.forEach((crr2, index2) => {
if (crr1 != crr2) {
newVal *= crr2;
}
});
result.push(newVal);
});
return result;
};
console.clear();
// Challenge Problem
console.log(`You have an array of integers, and for each index you want to find the product of every integer except the integer at that index. Write a function get_products_of_all_ints_except_at_index() that takes an array of integers and returns an array of the products. For example, given: [1, 7, 3, 4] Your function would return: [84, 12, 28, 21] By calculating: [7*3*4, 1*3*4, 1*7*4, 1*7*3] Do not use division in your solution.`);
// Examples
console.log(get_products_of_all_ints_except_at_index([1, 7, 3, 4]));
console.log(get_products_of_all_ints_except_at_index([10, 3, 23, 34]));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment