fix: provide initial values to reduce functions in generated js (#6178)

This commit is contained in:
gregdyke
2022-05-31 17:22:34 +01:00
committed by GitHub
parent f88b533686
commit 706c2bfd41
2 changed files with 4 additions and 4 deletions

View File

@@ -222,7 +222,7 @@ JavaScript['math_on_list'] = function(block) {
case 'SUM':
list = JavaScript.valueToCode(block, 'LIST',
JavaScript.ORDER_MEMBER) || '[]';
code = list + '.reduce(function(x, y) {return x + y;})';
code = list + '.reduce(function(x, y) {return x + y;}, 0)';
break;
case 'MIN':
list = JavaScript.valueToCode(block, 'LIST',
@@ -238,7 +238,7 @@ JavaScript['math_on_list'] = function(block) {
// mathMean([null,null,1,3]) === 2.0.
const functionName = JavaScript.provideFunction_('mathMean', `
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) {
return myList.reduce(function(x, y) {return x + y;}) / myList.length;
return myList.reduce(function(x, y) {return x + y;}, 0) / myList.length;
}
`);
list = JavaScript.valueToCode(block, 'LIST',

View File

@@ -476,7 +476,7 @@ function test_change() {
}
function mathMean(myList) {
return myList.reduce(function(x, y) {return x + y;}) / myList.length;
return myList.reduce(function(x, y) {return x + y;}, 0) / myList.length;
}
function mathMedian(myList) {
@@ -538,7 +538,7 @@ function mathRandomList(list) {
// Tests the "list operation" blocks.
function test_operations_on_list() {
assertEquals([3, 4, 5].reduce(function(x, y) {return x + y;}), 12, 'sum');
assertEquals([3, 4, 5].reduce(function(x, y) {return x + y;}, 0), 12, 'sum');
assertEquals(Math.min.apply(null, [3, 4, 5]), 3, 'min');
assertEquals(Math.max.apply(null, [3, 4, 5]), 5, 'max');
assertEquals(mathMean([3, 4, 5]), 4, 'average');