I made a turbowarp extension which is supposed to find the average number out of a list of numbers, but it didn't work. Here is my code:
class AverageExtension {
getInfo() {
return {
id: 'average',
name: 'Average Extension',
blocks: [
{
opcode: 'calculateAverage',
blockType: Scratch.BlockType.REPORTER,
text: 'calculate average of [list]',
arguments: {
list: {
type: Scratch.ArgumentType.LIST,
defaultValue: [0]
}
}
}
]
};
}
calculateAverage(args) {
const numList = Array.from(args.list);
if (numList.length === 0) {
return 0; // Return 0 for an empty list
}
const numericValues = numList.map(val => parseFloat(val)).filter(val => !isNaN(val));
if (numericValues.length === 0) {
return 0; // Return 0 if there are no valid numeric values
}
const sum = numericValues.reduce((acc, val) => acc + val, 0);
const average = sum / numericValues.length;
return average;
}
}
Scratch.extensions.register(new AverageExtension());
I tried asking chat gpt to fix it, but to no result, and instead of it working and giving me the average number, it just kept giving me errors an wrong results.