0

Hi I need help with the method that follows: var ages = [3, 10, 18, 20];

function checkAdult(age) { return age >= 18; }

function myFunction() { document.getElementById("demo").innerHTML = ages.findIndex(checkAdult); }

2
  • didn't get the proper answer Commented Jul 7, 2020 at 13:04
  • Please edit your existing post to clarify. Don't repost. Commented Jul 7, 2020 at 15:26

1 Answer 1

3

It's entirely possible that the values are null instead of an empty string. There is a difference between the two (JavaScript actually has both null and undefined types that represent a missing a value). Also, findIndex is not necessary here, you can just use find:

const test = !!data.find(value => value.Fname || value.Lname);

The !! notation converts a falsy value to false and a truthy value to true without a ternary operator. A value is falsy if it is null, undefined, empty string, 0, or false, and true otherwise. This should be sufficient for your use case.

Also, I noticed a typo in your code, which I've fixed, above. Lanme is probably meant to be Lname. Pay attention to typos, as that can easily cause hard-to-detect problems in JavaScript.

4
  • actually in the org if field value is blank then it is not present in the data aray. Commented Jul 7, 2020 at 13:49
  • @raman That would be undefined, not ''. Commented Jul 7, 2020 at 13:53
  • const test = !data.some(value => !(value.Fname || value.Lname)); @raman try this. Commented Jul 7, 2020 at 14:25
  • @sfdcfox Thnakyou soo much it is undefined!! eaten my head from 1 hour Commented Jul 7, 2020 at 14:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.