So I was snooping around in the editor to see if I could find an avg function, but I can’t seem to find it, neither in JavaScript nor Blocks. So after that, I decided to make it into an extension, but can anyone who has made extensions help me? This is what I have so far…
namespace Math {
//% block="avg $value"
export function avg(value: Array) {
let temporaryValue = 0
for (let index = 0; index < value.length; index++){
temporaryValue += index
}
return (temporaryValue / value.length)
}
Is there a better way to do this? I’ve hit this error with the playground editor:
Generic type ‘Array’ requires 1 or more argument(s)
Great extension idea! The error you’re running into is that value: Array should be value: number[]. Arrays are represented by square brackets, and the “type” of the array is written before the brackets, so as another example, an array of sprites would be value: Sprite[].
//% blockId=allOfKind block="array of sprites of kind $kind"
//% kind.shadow="spritekind"
export function allOfKind(kind: number): Sprite[] {
...
(spritekind is the blockid of the block that makes the dropdown of spritekinds, and shadow says 'make tthe thing here start as ‘spritekind’ / have that stay around even if they drag something else on top of it)
The return type of the function is wrong - its saying its returning an array of sprites (: Sprite[]) where you’re trying to return a boolean value. Just swap Sprite[] with boolean.
Also, you’ll need to change the blockId (and probably the function name too to give it something meaningful) - the example I gave was mostly copied from code in the editor by default, leaving it as allOfKind will conflict with the default sprite block.