Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //08. Condense Array to Number
- ///11 Arrays/08. Condense Array to Number.js
- function condenseArrayToNumber(originalArr) {
- 'use strict'
- let nums = originalArr
- while (nums.length > 1) {
- let condensed = Array(nums.length - 1);
- for (let i = 0; i < nums.length - 1; i++) {
- condensed[i] = +nums[i] + +nums[i + 1];
- }
- nums = condensed;
- }
- console.log(nums[0])
- }
- //Рекурсия
- function condenseArrayToNumber0(originalArr) {
- 'use strict'
- let nums = originalArr
- if (nums.length === 1) {
- console.log(nums[0])
- return
- }
- for (let i = 0; i < nums.length - 1; i++) {
- nums[i] = Number(nums[i]) + Number(nums[i + 1])
- }
- nums.pop()
- condenseArrayToNumber0(nums)
- }
- condenseArrayToNumber([1,2,3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement