Skip to content

Commit 4ef44f0

Browse files
committed
extendable math operators
1 parent 98ae84c commit 4ef44f0

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/blocks/scratch3_operators.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class Scratch3OperatorsBlocks {
2020
operator_subtract: this.subtract,
2121
operator_multiply: this.multiply,
2222
operator_divide: this.divide,
23+
operator_add_extendable: this.addExtendable,
24+
operator_subtract_extendable: this.subtractExtendable,
25+
operator_multiply_extendable: this.multiplyExtendable,
26+
operator_divide_extendable: this.divideExtendable,
2327
operator_lt: this.lt,
2428
operator_equals: this.equals,
2529
operator_gt: this.gt,
@@ -40,7 +44,7 @@ class Scratch3OperatorsBlocks {
4044
}
4145

4246
checkbox () {
43-
return true;
47+
return true;
4448
}
4549

4650
add (args) {
@@ -58,6 +62,26 @@ class Scratch3OperatorsBlocks {
5862
divide (args) {
5963
return Cast.toNumber(args.NUM1) / Cast.toNumber(args.NUM2);
6064
}
65+
66+
addExtendable (args, util) {
67+
const arr = util.extendableToArray(args, 'NUMS', 'NUM');
68+
return arr.reduce((a, b) => Cast.toNumber(a) + Cast.toNumber(b));
69+
}
70+
71+
subtractExtendable (args, util) {
72+
const arr = util.extendableToArray(args, 'NUMS', 'NUM');
73+
return arr.reduce((a, b) => Cast.toNumber(a) + Cast.toNumber(b));
74+
}
75+
76+
multiplyExtendable (args, util) {
77+
const arr = util.extendableToArray(args, 'NUMS', 'NUM');
78+
return arr.reduce((a, b) => Cast.toNumber(a) * Cast.toNumber(b));
79+
}
80+
81+
divideExtendable (args, util) {
82+
const arr = util.extendableToArray(args, 'NUMS', 'NUM');
83+
return arr.reduce((a, b) => Cast.toNumber(a) / Cast.toNumber(b));
84+
}
6185

6286
lt (args) {
6387
return Cast.compare(args.OPERAND1, args.OPERAND2) < 0;
@@ -103,13 +127,9 @@ class Scratch3OperatorsBlocks {
103127
return Cast.toString(args.STRING1) + Cast.toString(args.STRING2);
104128
}
105129

106-
joinExtendable (args) {
107-
let string = "";
108-
const argCount = +args.STRINGS;
109-
for (let i = 0; i < argCount; i++) {
110-
string += Cast.toString(args["STRINGS_" + i + "_STRING"]);
111-
}
112-
return string;
130+
joinExtendable (args, util) {
131+
const arr = util.extendableToArray(args, 'STRINGS', 'STRING');
132+
return arr.reduce((a, b) => a + Cast.toString(b), '');
113133
}
114134

115135
letterOf (args) {

src/compiler/compat-blocks.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ const stacked = [
3636
const inputs = [
3737
'motion_xscroll',
3838
'motion_yscroll',
39+
'operator_add_extendable',
40+
'operator_divide_extendable',
3941
'operator_join_extendable',
42+
'operator_multiply_extendable',
43+
'operator_subtract_extendable',
4044
'sensing_loud',
4145
'sensing_loudness',
4246
'sensing_userid',

src/engine/block-utility.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ class BlockUtility {
197197
return this.thread.getParam(paramName);
198198
}
199199

200+
/**
201+
* Gets all arguments of a specific name in an extendable input in an args object as an array.
202+
* @param {object} args The args object.
203+
* @param {string} arg The name of the extendable arugment.
204+
* @param {string} innerArg The name of the inner extendable arugment to get.
205+
* @returns {array} All
206+
*/
207+
extendableToArray (args, arg, innerArg) {
208+
let length = +args[arg];
209+
// NaN and negative
210+
if (!(length > 0)) length = 0;
211+
const array = new Array(length);
212+
for (let i = 0; i < length; i++) {
213+
array[i] = args[`${arg}_${i.toString()}_${innerArg}`];
214+
}
215+
return array;
216+
}
217+
200218
/**
201219
* Start all relevant hats.
202220
* @param {!string} requestedHat Opcode of hats to start.

0 commit comments

Comments
 (0)