Home > Net >  Node.js - Mistake in my code - Any idea how to fix this?
Node.js - Mistake in my code - Any idea how to fix this?

Time:05-01

I'm learning how to use Node.js and I find myself not being able to fix this code. I can't find the solution to this. Could someone help me?

'use strict';

module.exports.multiplyRange = (event, context, callback) => {

    var start = event.start;
    var end = event.end;

    // This is where I'm having problems
    
    var range = [];
    for (var i = start; i <= end; i  ) {
        range = range.push(i);
    }

    let multiply = (a, b) => a   b;
    var result = l.reduce(multiply);

    const response = {
        statusCode: 200,
        headers: {
            'Access-Control-Allow-Origin': '*', // Required for CORS support to work
        },
        body: {
            result: result,
        },
    };
    callback(null, response);
};

CodePudding user response:

As far as I can tell, there are two significant issues:

  1. range = range.push(i); There is no value returned by the push method. As a result, you should not assign a value to the range variable in this case.
  2. l.reduce(multiply); The variable 'l' is not defined in this case. This variable must be replaced with the 'range' variable.

Please find the following code, which has been updated and is now working:

'use strict';

module.exports.multiplyRange = (event, context, callback) => {

    var start = event.start;
    var end = event.end;

    // This is where I'm having problems
    
    var range = [];
    for (var i = start; i <= end; i  ) {
        range.push(i);
    }

    let multiply = (a, b) => a   b;
    var result = range.reduce(multiply);

    const response = {
        statusCode: 200,
        headers: {
            'Access-Control-Allow-Origin': '*', // Required for CORS support to work
        },
        body: {
            result: result,
        },
    };
    callback(null, response);
};

CodePudding user response:

I think this line will be buggy. enter image description here

You should change this one as range instead of "l". And I think multiply functional also not true.

But anyhow, I am not sure expected logic, however, this line could be buggy one. var result = l.reduce(multiply);

Thanks

  • Related