Ad
  • Default User Avatar

    read more on this correct me if i'm wrong but the second argument (array.fill) is the initial value that the reduce method is being applied to

  • Default User Avatar

    I don't see the code but to guess a bit -

    [].reduce(f, acc)

    acc is the initial value to use, for example when summing you start with 0:

    const f = (a, b) => a + b
    let acc = 0  // <-- second argument of reduce
    for (const x of xs) {
        acc = f(acc, x)
    }
    

    Two big reasons why one might want to supply this is if it's empty (otherwise there can be no result) or if the accumulated value has a different type/shape from the things in the array so that the first value of the array cannot be used as the initial value (which is what happens when omitting acc)

  • Default User Avatar

    i have a question/comment about this (sorry please humor a beginner)
    i get that you're adding next to the lowest prev
    i've just only seen reduce containing one function returning a single numeric value so this is my first exposure to a second argument being passed to the method
    is the idea that by using return prev in the function followed by array fill, it's sort of doing a micro-level reduction and replaces an index in the fill array?

  • Custom User Avatar

    You should rename "prev" and "next" to "checkouts" and "nextCustomer" or something like that... This confuses the way that reduce() works.