在节点js中使用函数

时间:2019-06-19 15:42:41

标签: node.js function

a)  Write an integer function Input as follows:

         function Input(s)

The function takes in a String parameter that will be used as part of the input  prompt, e.g. "Please enter 1st integer" and "Please enter 2nd integer". It returns an integer value corresponding to the user input.           

b)  Write a function bigger that takes in 2 integer values first and second. It compares which number is bigger and returns one of the following string values:
            "1st number is bigger"
            "2nd number is bigger"
            "The 2 numbers are equal"

c)  In the main part of the program, invoke Input(“1st”) and Input(“2nd”) to get the values of first and second. Invoke bigger and display the string returned.

我的代码遇到了一些问题,因为它似乎不起作用,因为我不知道如何将输入链接在一起。这是我的实际问题之一。

我尝试将输入链接在一起,但是我不确定该怎么做,因为问题有点令人困惑。

我不太了解问题要求,所以我不知道如何描述我的问题

1 个答案:

答案 0 :(得分:0)

我怀疑您对此有误解。

根据描述,您的主程序不需要比这更复杂:

let a = Input("1st");
let b = Input("2nd");
console.log(bigger(a, b));

现在,您“只是”需要实现Inputbigger


附录:将您已有的代码放入函数中并不是很复杂:

function Input(prompt)
{
    return parseInt(input.question("Please enter " + prompt + " number:"));
}

function bigger(a, b)
{
    if (a > b) {
        return "1st number is bigger";
    }
    else if (a < b) {
        return "2nd number is bigger";
    }
    else {
        return "The two numbers are equal";
    }
}

与上面的代码一起使用。