0

I am enabled to validate a function if it is an array of two or more value

const validateZIPLength = (zip) => (
  zip.length > 5 || zip.length < 5 ?
    'zip code should be 5 digits': undefined
)
validateZIPLength('123')
for the above function cal, it works fine

validateZIPLength(['1234', '12345'])

it should return the zip code should be 5 and undefined but it is returning only zip code should be 5 for the 1st item in the array

3
  • 2
    ['1234', '12345'].map(validateZIPLength) Commented May 11, 2018 at 16:17
  • the problem is that if it is single address this approach works fine validateZIPLength('123') but how to write a check condition in the function itself if there is an array of zip's Commented May 11, 2018 at 16:47
  • If you can't control how the zip is going to be passed to the function validateZIPLength then you could put a check inside the function to see if what is passed is an array. It would look something like this: cont validateZIPLength = (zip) => ( if (Array.isArray(arguments[0])) { arguments[0].map((z) => { zip.length > 5 || zip.length < 5 ? 'zip code should be 5 digits': undefined }) } else { Code for if just the zip } Commented May 11, 2018 at 16:55

2 Answers 2

1

Your function can only handle individual values, not arrays.

Here's how you can add support for arrays :

function validateZIPLength(zip) {
  if(Array.isArray(zip)) {
    for(let i = 0; i < zip.length; i++) {
      zip[i] = validateZIPLength(zip[i]);
    }
    return zip;
  } else {
    return zip.length === 5 ? undefined : "zip code should be 5 digits";
  }
}

console.log(validateZIPLength(['1234', '12345']));

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

6 Comments

Thanks for the help the function seems to work fine and I was expecting this functionality
when I'm trying to use this function a lint error is thrown stating Unexpected negated condition
thanks for the help but the still same issue at this line Unexpected negated condition else { return zip.length !== 5 ? "zip code should be 5 digits" : undefined;
@viky : I made another change to the code. Could you try again? I'm pretty sure it works now. — And if you're still having an issue, could you tell me what linter you're using?
Hi john when i'm using the above function it is returning undefined is there any means that if there are no errors can i return an empty array instead of undefined in an array
|
0

validateZIPLength(['1234', '12345'])

it should return the zip code should be 5 and undefined but it is returning only zip code should be 5 for the 1st item in the array

Actually, what happens is that the function validateZIPLength returns what you get for the entire array itself, not for the first element. When you pass in the array, it doesn't know if it got a string or an array, it simply checks the length as per zip.length > 5 || zip.length < 5 and since an array also has a length property, the code works as you instructed it - it gets 2 because there are two elements in the array and thus returns you the string 'zip code should be 5 digits'.

Remember, computers are stupid - they do what you tell them to, not necessarily what you want them to.

Since you have a function that takes strings and gives you results, but you want to apply it against many elements of an array, you can use .map() which does exactly that for you. It takes a function and calls it with each member of the array. You then get a new array that contains each result.

const validateZIPLength = (zip) => (
  zip.length > 5 || zip.length < 5 ?
    'zip code should be 5 digits': undefined
)
const singleInvalidZIPCode = validateZIPLength('123');
const singleValidZIPCode = validateZIPLength('12345');

console.log("single invalid ZIP code:", singleInvalidZIPCode);
console.log("single valid ZIP code:", singleValidZIPCode);

const multipleZIPCodes = ['1234', '12345'].map(validateZIPLength)

console.log("multiple ZIP codes:", multipleZIPCodes);

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.