Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function get_products_of_all_ints_except_at_index_verbose(productArray) {
  2. return productArray.map((value, i, array) => {
  3. // Wanted to make copies of the array without the element at the current index.
  4. // .splice returns an array of the deleted items and modifies the original.
  5. // This is not useful for chaining functions together.
  6. let newArray = array.slice();
  7. newArray.splice(i, 1);
  8. return newArray.reduce((p, x) => p * x);
  9. });
  10. }
  11.  
  12. function get_products_of_all_ints_except_at_index_wrong(productArray) {
  13. return productArray.map((value, i, array) => array.reduce((p, x) => value != x ? p * x : p, 1));
  14. }
  15.  
  16. function get_products_of_all_ints_except_at_index_good(productArray) {
  17. return productArray.map((value, i, array) => array.reduce((p, x, j) => i != j ? p * x : p, 1));
  18. }
  19.  
  20. let get_products_of_all_ints_except_at_index = get_products_of_all_ints_except_at_index_good;
  21.  
  22. let array = [1, 7, 3, 4];
  23. let result = get_products_of_all_ints_except_at_index(array);
  24. console.log(result);
  25.  
  26. array = [10, 7, 3, 4, 4];
  27. result = get_products_of_all_ints_except_at_index(array);
  28. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement