在控制台 javascript 函数中处理错误和日志消息

handle error and log message in console javascript function

我有数组 [1, 2, 3]

Create function hs that takes this array as parameter and then displays elements of this array on the screen. Use the loops is prohibited. This function has to validate an input parameter, because function can accept only a non-empty array.

f(1,2,3) // Error: parameter type should be an array

您可以试试下面的代码

function hs(array, ...args) {
    if(args && args.length > 0) {
       console.log('The function takes only one input');
    } else if (!array || !Array.isArray(array)) {
       console.log('parameter type should be an array');
    }
}