1

I am actually having an issue, more with the instructions for this homework problem which are as follows:

Reduces collection to a value which is the accumulated result of running each element in collection through iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not provided the first element of collection is used as the initial value. If a start parameter is not provided, then set the start value as the zeroth index.

here is the breakdown of what is supposed to happen:

// reduce([1,2], function(stored,current) {
//  return stored + current;
// }); → 3
// reduce([1,2], function(stored,current) {
//  return stored + current;
// },1); → 4

if i'm reading the instructions correctly, the start parameter is the initial value to start accumulating to, no? and if no start parameter is supplied in the call to the function, that the value at index 0 will be the start value. Only problem is when that value is grabbed in cases where no start parameter is supplied, the first val is accumulated twice, which returns a result that is not expected.

as of now i've looked into the reduce method and different ways of starting at different indices. it's looking like if no start value is provided, that I would need to set the initial value to array[0] and then array.slice(1) and reduce from there, however, i'm not really sure that's what the assignment says to do.

I'm not quite understanding the difference between if an accumulator is not provided and if a start parameter is not provided. If an accumulator isn't provided, wouldn't the initial value be the first value in the input array, and the starting index be 1? so to not add/subtract the first value twice?

here's my code:

function reduce(array, callback, start) { 
    return array.reduce((acc, val, start) => {
      //if start value not provided, start value is index 0
        return callback(acc, val) }, start || array[0])
}

here are results.

//start provided as -1, result correct
var difference = function(tally, item) {return tally - item; };
var total = reduce([1, 2, 3], difference, -1); // expected -> -7 got -7

//start provded as 2, result correct
var add = function(tally, item) {return tally + item; };
var total = reduce([1, 2, 3], add, 2); // expected -> 8 got 8

//start not provided, first index used as start, as per instructions
//therefore first index is added twice, giving wrong answer
var add = function(tally, item) {return tally + item; };
var total = reduce([1, 2, 3], add); // expected -> 6 got 7

//start not provided, first index used as start, as per instructions
//therefore first index is subtracted twice, giving wrong answer
var difference = function(tally, item) { return tally - item; };
var total = reduce([1, 2, 3], difference); // -> expected -4 got -5
0

2 Answers 2

1

Yes, you are right. If no initial accumulator is provided, the first array element is taken as an accumulator, and the callback will be called from the second element onwards.

Your code duplicates the first array element though as it passes it as an accumulator. Also using || is dangerous here, it will fail for e.g. 0. I'd just do:

 function reduce(array, callback, start) { 
   return array.reduce(callback, start);
 }
Sign up to request clarification or add additional context in comments.

Comments

0

It's a little strange to make a reduce function to just call the reduce builtin. If the goal is to write your own reduce, it's probably a little cleared if you explicitly make the loop.

Based on whether start is given, you will need to make two choices: the initial value of the accumulator and whether to start iterating on the first or second item of the array:

function reduce(array, callback, start) { 
    let start_index = start ? 0 : 1                   // start iteration on second item if start not given
    let acc = start === undefined ? array[0] : start  // use array[0] if start not given

    for (let i = start_index; i < array.length; i++){
        acc  = callback(acc, array[i])
    }
    return acc
}
var add = function(tally, item) {return tally + item; };

var total = reduce([1, 2, 3], add); // expected -> 6 got 7
console.log(total) // 6 as expected

var difference = function(tally, item) { return tally - item; };
var total = reduce([1, 2, 3], difference); // -> expected -4 got -5
console.log(total)  // -4 as expected

1 Comment

i see it makes sense now that they are just wanting me to rebuild reduce from 'scratch'. thank you for this. I will take another stab at it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.