Advertisement
Spocoman

01. Add and Subtract

Jan 22nd, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.  
  3.     let originalSum = 0;
  4.     let modifiedSum = 0;
  5.  
  6.     for (let i = 0; i < arr.length; i++) {
  7.         originalSum += arr[i];
  8.     }
  9.     for (let i = 0; i < arr.length; i++) {
  10.         arr[i] % 2 === 0 ? arr[i] += i : arr[i] -= i;
  11.     }
  12.     for (let i = 0; i < arr.length; i++) {
  13.         modifiedSum += arr[i];
  14.     }
  15.     console.log(arr, `\n` + originalSum, `\n` + modifiedSum);
  16. }
  17.  
  18.  
  19. Решение с lambda:
  20.  
  21. function solve(arr) {
  22.  
  23.     let originalSum = arr.reduce((a, b) => a + b, 0);
  24.  
  25.     for (let i = 0; i < arr.length; i++) {
  26.         arr[i] % 2 === 0 ? arr[i] += i : arr[i] -= i;
  27.     }
  28.  
  29.     let modifiedSum = arr.reduce((a, b) => a + b, 0);
  30.  
  31.     console.log(arr, `\n` + originalSum, `\n` + modifiedSum);
  32.  
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement