Help with sum of block

so, I was making the sum of block in mathematics. but how do you make that in makecode cause I I’m confused how to make sum of into a block.
here’s the code:

    //% block="Sum of $index|from $num|to $num2| with block input"
    //% draggableParameters
    export function Sumof(num: number, num2: number, num3: number, handler: (index: number) => void) {
        return handler;
    }

Summation - Wikipedia

1 Like

You could conceptually make the block, but only in JS/TS. The summation logic itself is fine; the limitation is with MakeCode Blocks.

In TypeScript, you can use a callback that returns a number:

//% block="Sum of $index|from $num|to $num2| with block input"
//% draggableParameters
export function Sumof(
    num: number,
    num2: number,
    handler: (index: number) => number
): number {
    let sum = 0

    for (let index = num; index <= num2; index++) {
        sum += handler(index)
    }

    return sum
}

You could then use it like this:

let result = Sumof(1, 5, function (index) {
    return index * index
})

That calculates:

1² + 2² + 3² + 4² + 5² = 55

So this part of the original code is correct:

handler: (index: number) => number

The problem is that MakeCode Blocks cannot currently represent a callback that has a draggable variable like index and also returns a number that can be used as a reporter block.

That is why it either creates an empty callback:

Sumof(0, 0, function (index) {

})

or removes the callback completely when you put the block into a variable:

Sumof(0, 0)

You can still make a statement-style summation block in Blocks, but a true sigma block that takes an expression and returns the sum needs to be used in TypeScript, or redesigned for Blocks using something like a separate summation result variable.

I have tried doing this, but like I said, MakeCode’s block is just limited with the PXT.

4 Likes

hmm… if you mean Σ (sigma), then maybe this:

function summation(min: number, max: number, handler: (index: number) => number) {
    let sum = 0;
    for (let i = min; i <= max; i++) sum += handler(i);
    return sum;
}

note that it wouldn’t work in blocks, but if you want it to I’m allowing you to still my method of returning values in this

2 Likes

Also I’m trusting you can make the product notation (PI → Π)

1 Like

and what I mean by that is:

namespace CyberScripts {
    let lastSum = 0
    export function getSum() { return lastSum }

    export function summation(min: number, max: number, handler: (index: number) => number) {
        lastReturn = 0
        for (let i = min; i <= max; i++) {
            handler(i)
            lastReturn += lastRet
        }
    }


    let lastRet = 0
    export function returnVal(n: number) { lastSum = n }
}

example of use:

CyberScripts.summation(1, 5, function (i) {
    CyberScripts.returnVal(i ** 2) // i^2
})
console.log(CyberScripts.getSum())

that would work completely fine in blocks. Doing this:

// assumes CyberScripts.summation returns the sum
console.log(
    CyberScripts.summation(1, 5, function (i) {
        return i ** 2 // i^2
    })
)

would not work in blocks.

what the heck did I write oops,

namespace CyberScripts {
    let lastSum = 0
    export function getSum() { return lastSum }

    export function summation(min: number, max: number, handler: (index: number) => number) {
        lastSum = 0
        for (let i = min; i <= max; i++) {
            handler(i) // calls returnVal, setting lastRet
            lastSum += lastRet
        }
    }


    let lastRet = 0
    export function returnVal(n: number) { lastRet = n }
}

@CodaKnight correct you cant return values in blocks funtions in handlers, but like my post, you can get around it.