1

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 :/

3 Answers 3

1

You are almost there. Just a simple problem in the first if statement. You need to check strictly. Then you also need to check for '0' because the description of problems allows an integer-formatted string

if(n === 0 || n === '0') return [];

The problem n is false then n == 0 evaluates to true so an empty array is returned. But you need to throw error in case of false.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation, I m still new to coding so I don't see all the subtleties yet ^^'
Although not explicitly stated as problem, the solution does not pass one test in link challenge: Test.assertSimilar(prefill('1',1), [1]); and hence does not fulfill the required anything other than an integer or integer-formatted string (e.g. '123') that is >=0.
1

The issue seems to be that Number.isInteger(n) expects a number not a string. If you use parseInt(n) all works.

Also the difference between == and === is that: == converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

In general it's best to use === but in your case, since you are comparing integers and strings to 0 you could use if(n == 0) return [];

function prefill(n, v) {
  if(n === 0 || n === '0') return [];

  if(!parseInt(n) || n < 0 ){

    let typeError = new TypeError();
    typeError.name = "TypeError";
    typeError.message =  n + " is invalid";
    throw typeError
  };

  return new Array(n).fill(v); 
}

Test Passed: Value == '[1, 1, 1]'
Test Passed: Value == '[\'abc\', \'abc\']'
Test Passed: Value == '[1]'
Test Passed: Value == '[[\'2d\', \'2d\'], [\'2d\', \'2d\'], [\'2d\', \'2d\']]'
Test Passed: Value == 'TypeError'
Test Passed: Value == 'xyz is invalid'
Test Passed
You have passed all of the tests! :)

1 Comment

Thoroughly solved! Your solution also passes the failing test: Test.assertSimilar(prefill('1',1), [1]);, although not explicitly as problem by the OP.
0

The solution above did not pass the "should throw errors with n as a float" test. Instead of using !parseInt(n), I used !Number.isInteger(n) found in this solution.

Here is my full answer:

function prefill(n, v) {
    // Return empty array if 0
    if (n === 0 || n === '0') return [];

    // Throw error if below 0 or not an integer 
    if (n < 0 || !Number.isInteger(n)) {
        let typeError = new TypeError();
        typeError.name = "TypeError";
        typeError.message = n + " is invalid";
        throw typeError
    };

    return new Array(n).fill(v);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.