/**
* Takes only positives values from an array
*
* @params {number[]} numbers Numbers's array
*
* @throws {Error} If the function is not called with one argument
* @throws {TypeError} If the first argument is not an array
* @throws {TypeError} If the first argument is not an array of numbers
*
* @return {number[]} Positives numbers
*
* @example
* positivesOnly([-1, 1, -2, 2]); // [1, 2]
* positivesOnly([1, 2]); // [1, 2]
* positivesOnly([-1, -2]); // []
*/constpositivesOnly=(numbers,...extraParameters)=>{if(extraParameters.length!==0){thrownewError("expected one argument");}if(!Array.isArray(numbers)){thrownewTypeError("expected first argument to be an array");}if(numbers.length===0){return[];}const[number,...rest]=numbers;if(typeofnumber!=="number"){thrownewTypeError("expected first argument to be an array of numbers");}if(number<0){returnpositivesOnly(rest);}return[number,...positivesOnly(rest)];};
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My take at the challenge using recursion.