Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

4.2 KiB

title
title
Everything Be True

Everything Be True


Problem Explanation

The program needs to check if the second argument is a truthy element, and it must check this for each object in the first argument.


Hints

Hint 1

Remember to iterate through the first argument to check each object.

Hint 2

Only if all of them are truthy will we return true, so make sure all of them check.

Hint 3

You could use loops or callback functions, there are multiple ways to solve this problem.


Solutions

Solution 1 (Click to Show/Hide)

Using for-in loop & hasOwnProperty

function truthCheck(collection, pre) {
  // Create a counter to check how many are true.
  var counter = 0;
  // Check for each object
  for (var c in collection) {
    // If it is has property and value is truthy
    if (collection[c].hasOwnProperty(pre) && Boolean(collection[c][pre])) {
      counter++;
    }
  }
  // Outside the loop, check to see if we got true for all of them and return true or false
  return counter == collection.length;
}

// test here
truthCheck(
  [
    { user: "Tinky-Winky", sex: "male" },
    { user: "Dipsy", sex: "male" },
    { user: "Laa-Laa", sex: "female" },
    { user: "Po", sex: "female" }
  ],
  "sex"
);

Code Explanation

  • First I create a counter to check how many cases are actually true.
  • Then check for each object if the value is truthy
  • Outside the loop, I check to see if the counter variable has the same value as the length of collection, if true then return true, otherwise, return false
Solution 2 (Click to Show/Hide)

Using Array.every()

function truthCheck(collection, pre) {
  return collection.every(function(element) {
    return element.hasOwnProperty(pre) && Boolean(element[pre]);
  });
}

// test here
truthCheck(
  [
    { user: "Tinky-Winky", sex: "male" },
    { user: "Dipsy", sex: "male" },
    { user: "Laa-Laa", sex: "female" },
    { user: "Po", sex: "female" }
  ],
  "sex"
);

Code Explanation

  • Uses the native "every" method to test whether all elements in the array pass the test.
  • This link will help Array.prototype.every()
Solution 3 (Click to Show/Hide)
function truthCheck(collection, pre) {
  // Is everyone being true?
  return collection.every(obj => obj[pre]);
}

truthCheck(
  [
    { user: "Tinky-Winky", sex: "male" },
    { user: "Dipsy", sex: "male" },
    { user: "Laa-Laa", sex: "female" },
    { user: "Po", sex: "female" }
  ],
  "sex"
);

Code Explanation

  • For every object in the collection array, check the truthiness of object's property passed in pre parameter
  • Array#every method internally checks if the value returned from the callback is truthy.
  • Return true if it passes for every object. Otherwise, return false.