Nodejs:挑战从STDIN读取add(2)(3)和add(2,3),并在STDOUT上返回总和

Nodejs: Challenge to read add(2)(3) and add(2,3) from STDIN and return the sum on STDOUT

本文关键字:add STDOUT 返回 并在 Nodejs STDIN 挑战 读取      更新时间:2023-09-26

我的朋友刚刚让我解决这个问题:在STDIN中输入以下内容:
add (2) (3)
添加(2、3)

并在STDOUT上得到以下输出:

55

嗯,我不知道如何在nodejs平台上做到这一点。我在javascript中有以下代码,但它没有给我任何输出。我也不确定这个代码是否正确。

var add = function (a) {
return function (b) {return a + b;};
};


注意:我必须从STDIN调用这个函数。因为我是新的nodejs我只是不知道如何得到这个完成

您正在寻找的最基本形式的函数可能是这个。这可以很容易地扩展为接受任何数量的数字。

var add = function add() {
    var cache;
    if (arguments.length === 1) {
        cache = arguments[0];
        return function ( number ) {return cache + number;};
    }
    else return arguments[0] + arguments[1];
};

你得自己弄清楚整个过程。stdin可以工作,因为我还没有使用它。