Advertisement
dabidabidesh

Condense Array Recursion

May 30th, 2020
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //08. Condense Array to Number
  2. ///11 Arrays/08. Condense Array to Number.js
  3. function condenseArrayToNumber(originalArr) {
  4.   'use strict'
  5.  
  6.   let nums = originalArr
  7.  
  8.   while (nums.length > 1) {
  9.     let condensed = Array(nums.length - 1);
  10.     for (let i = 0; i < nums.length - 1; i++) {
  11.       condensed[i] = +nums[i] + +nums[i + 1];
  12.     }
  13.     nums = condensed;
  14.   }
  15.   console.log(nums[0])
  16. }
  17. //Рекурсия
  18. function condenseArrayToNumber0(originalArr) {
  19.   'use strict'
  20.  
  21.   let nums = originalArr
  22.  
  23.   if (nums.length === 1) {
  24.     console.log(nums[0])
  25.     return
  26.   }
  27.   for (let i = 0; i < nums.length - 1; i++) {
  28.     nums[i] = Number(nums[i]) + Number(nums[i + 1])
  29.   }
  30.   nums.pop()
  31.   condenseArrayToNumber0(nums)
  32. }
  33. condenseArrayToNumber([1,2,3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement