Skip to content

Commit

Permalink
data-analysis: Add sum block (#1448)
Browse files Browse the repository at this point in the history
It's not uncommon to need the sum of a bunch of numbers.
  • Loading branch information
DNin01 committed May 9, 2024
1 parent edcd9c4 commit c343254
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions extensions/qxsck/data-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
id: "qxsckdataanalysis",
name: Scratch.translate({ id: "name", default: "Data Analysis" }),
blocks: [
{
opcode: "sum",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate({
id: "sum",
default: "sum of [NUMBERS]",
}),
arguments: {
NUMBERS: {
type: Scratch.ArgumentType.STRING,
defaultValue: "1 2 3 4 5",
},
},
},
{
opcode: "average",
blockType: Scratch.BlockType.REPORTER,
Expand Down Expand Up @@ -100,6 +114,13 @@
};
}

sum(args) {
const numbers = Scratch.Cast.toString(args.NUMBERS)
.split(" ")
.map(Number);
return numbers.reduce((a, b) => a + b, 0);
}

average(args) {
const numbers = Scratch.Cast.toString(args.NUMBERS)
.split(" ")
Expand Down

0 comments on commit c343254

Please sign in to comment.