Search code examples
javascriptarraysfunctionhandle

handle error and log message in console javascript function


i have the array with the numbers [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

Solution

  • You can try this code bellow

    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');
        }
    }