0
let list = {
    value: 15,
    rest: {
        value: 15,
        rest: {
            value: 15,
            rest: null
        }
    }
};

i want to get the sum of all values. How can i do this ?

songhee24
  • 129
  • 9

4 Answers4

1

You have at least two options:

  1. Recursion

  2. Loop

Recursion

With recursion, you can write a function for this that will call itself (recurse) when necessary.

For instance:

function sumValues(list) {
    let sum = list.value;
    if (list.rest) {
        // Recurse
        sum += sumValues(list.rest);
    }
    return sum;
}

Live Example:

let list = {
    value: 15,
    rest: {
        value: 15,
        rest: {
            value: 15,
            rest: null
        }
    }
};

function sumValues(list) {
    let sum = list.value;
    if (list.rest) {
        // Recurse
        sum += sumValues(list.rest);
    }
    return sum;
}

console.log(sumValues(list));

That can be written in a shorter way, but I wanted to emphasize the steps above:

function sumValues(list) {
    return list.value + (list.rest ? sumValues(list.rest) : 0);
}

Live Example:

let list = {
    value: 15,
    rest: {
        value: 15,
        rest: {
            value: 15,
            rest: null
        }
    }
};

function sumValues(list) {
    return list.value + (list.rest ? sumValues(list.rest) : 0);
}

console.log(sumValues(list));

Loops

This particular structure, though, works just fine with a loop:

function sumValues(list) {
    let sum = 0;
    for (let entry = list; entry; entry = entry.rest) {
        sum += entry.value;
    }
    return sum;
}

Live Example:

let list = {
    value: 15,
    rest: {
        value: 15,
        rest: {
            value: 15,
            rest: null
        }
    }
};

function sumValues(list) {
    let sum = 0;
    for (let entry = list; entry; entry = entry.rest) {
        sum += entry.value;
    }
    return sum;
}

console.log(sumValues(list));

Or with while:

function sumValues(list) {
    let sum = 0;
    let entry = list;
    while (entry) {
        sum += entry.value;
        entry = entry.rest;
    }
    return sum;
}

Live Example:

let list = {
    value: 15,
    rest: {
        value: 15,
        rest: {
            value: 15,
            rest: null
        }
    }
};

function sumValues(list) {
    let sum = 0;
    let entry = list;
    while (entry) {
        sum += entry.value;
        entry = entry.rest;
    }
    return sum;
}

console.log(sumValues(list));
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • thanks bro very much helped out, but there are still other solutions? – songhee24 May 23 '21 at 11:40
  • @AzamatOrozbaev - A simple loop does it as well, and is probably better suited to this specific data structure. I added that. Recursion is often interchangeable with looping, but one or the other can be more complex depending on the structure you're working with. – T.J. Crowder May 23 '21 at 12:02
  • One has to consider the stack size growth when employing recursion because Javascript interpreters do not optimize tail calls. https://stackoverflow.com/questions/37224520/are-functions-in-javascript-tail-call-optimized – loop May 23 '21 at 13:37
  • @loop - Indeed. Though some JavaScript engines¹ do tail call optimization -- JavaScriptCore (in Safari and **all** iOS browsers) for instance. (¹ We don't call them interpreters, not least because usually they aren't or, as in V8's case, the interpreter is only part of the story, the other part being a JIT compiler.) – T.J. Crowder May 23 '21 at 13:53
  • Well I specifically chose to use the word and will continue to do so. The Safari case is covered on the page I linked. – loop May 23 '21 at 14:07
  • @loop - Why use "interpreter" when it's incorrect? Also, again, it's not just Safari. **Every** browser on iOS uses JavaScriptCore (because they aren't allowed not to, because only Apple apps can allocate runnable memory, and JavaScript engines mostly aren't interpreters). – T.J. Crowder May 23 '21 at 14:22
  • Shall be left as an exercise to the reader. – loop May 23 '21 at 19:00
0

Using recursion is a possible way of iterating through all of the nested objects.

let list = {
    value: 15,
    rest: {
        value: 15,
        rest: {
            value: 15,
            rest: null
        }
    }
};

const sum = (list) => {
    let acc = list.value;
    if (list.rest) {
        acc += sum(list.rest);
    }
    
    return acc;
}


var res = sum(list);

console.log(res);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
0

Iterative and recursive solutions with a destructuring assignment.

let list = {
    value: 3,
    rest: {
        value: 7,
        rest: {
            value: 15,
            rest: null
        }
    }
};

function sum_iteratively({ value, rest }) {
  while(rest) {
    value += rest.value;
    rest = rest.rest;
  }
  return value;
}

function sum_recursively({ value, rest }) {
  return value + (rest?sum_recursively(rest):0);
}

console.log(sum_iteratively(list));
console.log(sum_recursively(list));
.as-console-wrapper { top: 0; max-height: 100% !important; }
loop
  • 825
  • 6
  • 15
-1

Recursive Approcah Can be Used Like This

function findSum(list){
    sum = list.value;
    if(list.rest){
        sum += findSum(list.rest);
    }
    return sum;
}
console.log(findSum(list));

OR More Simple Like This

function findSum(list){
    return list.rest ? findSum(list.rest) + list.value : list.value;
}