Here is the page link of the exercise: https://www.codewars.com/kata/prefill-an-array/train/javascript
Create a function prefill that returns an array of n elements all having the same value v. See if you can do this without using a loop.
You have to validate input:
v can be anything (primitive or otherwise) if v is omitted, fill the array with undefined if n is 0, return an empty array if n is anything other than an integer or integer-formatted string (e.g. '123') that is >=0, throw a TypeError When throwing a TypeError, the message should be "n is invalid", where you replace n for the actual value passed to the function.
my code:
function prefill(n, v) {
if(n == 0) return [];
if(!Number.isInteger(n) || n < 0 ){
let TypeError = new Error;
TypeError.name = "TypeError";
TypeError.message = n + " is invalid";
throw TypeError
};
return new Array(n).fill(v);
}
It passes all the test except this one: should throw an error with n as boolean Test Passed: Value == 'TypeError' Test Passed: Value == 'true is invalid' Test Passed prefill did not throw an error with n as false
Can anybody explain me why? I even tried inputting directly false and it did not work :/