diff --git a/src/blocks/scratch3_operators.js b/src/blocks/scratch3_operators.js index 4fd6a7bf42b..a06bfccb763 100644 --- a/src/blocks/scratch3_operators.js +++ b/src/blocks/scratch3_operators.js @@ -21,6 +21,7 @@ class Scratch3OperatorsBlocks { operator_multiply: this.multiply, operator_divide: this.divide, operator_lt: this.lt, + operator_exponent: this.exponent, operator_equals: this.equals, operator_gt: this.gt, operator_and: this.and, @@ -52,6 +53,10 @@ class Scratch3OperatorsBlocks { divide (args) { return Cast.toNumber(args.NUM1) / Cast.toNumber(args.NUM2); } + exponent (args) { + return Cast.toNumber(args.NUM1) ** Cast.toNumber(args.NUM2); +} + lt (args) { return Cast.compare(args.OPERAND1, args.OPERAND2) < 0; diff --git a/test/unit/blocks_operators.js b/test/unit/blocks_operators.js index 5dbe082e941..0dad6ed4b2e 100644 --- a/test/unit/blocks_operators.js +++ b/test/unit/blocks_operators.js @@ -32,6 +32,12 @@ test('divide', t => { t.end(); }); +test('exponent', t => { + t.strictEqual(blocks.exponent({NUM1: '2', NUM2: '3'}), 8); + t.strictEqual(blocks.exponent({NUM1: 'foo', NUM2: 'bar'}), 1); + t.end(); +}); + test('lt', t => { t.strictEqual(blocks.lt({OPERAND1: '1', OPERAND2: '2'}), true); t.strictEqual(blocks.lt({OPERAND1: '2', OPERAND2: '1'}), false); diff --git a/test/unit/blocks_operators_infinity.js b/test/unit/blocks_operators_infinity.js index 96771ab986f..4e324ca0f18 100644 --- a/test/unit/blocks_operators_infinity.js +++ b/test/unit/blocks_operators_infinity.js @@ -11,6 +11,46 @@ test('divide: (1) / (0) = Infinity', t => { t.end(); }); +test('exponent: exponentiation with Infinity', t => { + t.strictEqual( + blocks.exponent({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" ^ 111 = Infinity' + ); + t.strictEqual( + blocks.exponent({NUM1: 'INFINITY', NUM2: 222}), 0, '"INFINITY" ^ 222 = 0' + ); + t.strictEqual( + blocks.exponent({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity ^ 333 = Infinity' + ); + t.strictEqual( + blocks.exponent({NUM1: 111, NUM2: 'Infinity'}), Infinity, '111 ^ "Infinity" = Infinity' + ); + t.strictEqual( + blocks.exponent({NUM1: 222, NUM2: 'INFINITY'}), 1, '222 ^ "INFINITY" = 1' + ); + t.strictEqual( + blocks.exponent({NUM1: 333, NUM2: Infinity}), Infinity, '333 ^ Infinity = Infinity' + ); + t.strictEqual( + blocks.exponent({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" ^ 111 = -Infinity' + ); + t.strictEqual( + blocks.exponent({NUM1: '-INFINITY', NUM2: 222}), 0, '"-INFINITY" ^ 222 = 0' + ); + t.strictEqual( + blocks.exponent({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity ^ 333 = -Infinity' + ); + t.strictEqual( + blocks.exponent({NUM1: 111, NUM2: '-Infinity'}), 0, '111 ^ "-Infinity" = 0' + ); + t.strictEqual( + blocks.exponent({NUM1: 222, NUM2: '-INFINITY'}), 1, '222 ^ "-INFINITY" = 1' + ); + t.strictEqual( + blocks.exponent({NUM1: 333, NUM2: -Infinity}), 0, '333 ^ -Infinity = 0' + ); + t.end(); +}); + test('divide: division with Infinity', t => { t.strictEqual( blocks.divide({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" / 111 = Infinity'