Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_products_of_all_ints_except_at_index_verbose(productArray) {
- return productArray.map((value, i, array) => {
- // Wanted to make copies of the array without the element at the current index.
- // .splice returns an array of the deleted items and modifies the original.
- // This is not useful for chaining functions together.
- let newArray = array.slice();
- newArray.splice(i, 1);
- return newArray.reduce((p, x) => p * x);
- });
- }
- function get_products_of_all_ints_except_at_index_wrong(productArray) {
- return productArray.map((value, i, array) => array.reduce((p, x) => value != x ? p * x : p, 1));
- }
- function get_products_of_all_ints_except_at_index_good(productArray) {
- return productArray.map((value, i, array) => array.reduce((p, x, j) => i != j ? p * x : p, 1));
- }
- let get_products_of_all_ints_except_at_index = get_products_of_all_ints_except_at_index_good;
- let array = [1, 7, 3, 4];
- let result = get_products_of_all_ints_except_at_index(array);
- console.log(result);
- array = [10, 7, 3, 4, 4];
- result = get_products_of_all_ints_except_at_index(array);
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement