Everything Be True Algorithm

Hey guys,

Looking for some feedback on my solution to the “Everything Be True” algorithm.

I know the Wiki doesn’t include every solution imaginable, but the one I used to solve this lesson isnt there and it’s quite simple, so I’m just wondering if it possibly wouldn’t work for all scenarios (ie. one’s not provided by FCF to test the algorithm).

function truthCheck(collection, pre) {
  for (var i=0;i<=collection.length-1;i++){
    if (collection[i][pre]){
      continue;
    } else {
      return false;
    }
  }
  return true;
} 

Does this solution seem okay?

Thanks,

Sean

edit: Updated code to simplify/remove continue statement:

function truthCheck(collection, pre) {
  for (var i=0;i<=collection.length-1;i++){
    if (!collection[i][pre]){
      return false;
    }
  }
  return true;
}

It’s not bad, but I find that anytime I use a keyword like continue (that is, something you don’t see very often in code), it means I can change my code for the better. These sorts of clues are called “code smells” - they’re not necessarily bad, but they can indicate areas of improvement. In this case, continue is what a for loop does unless you tell it to do something else, so if you could switch the return false and continue statements, you could remove the else entirely. How would you change the logic of your if clause to make that work?

Thanks for the feedback! I edited my original post to reflect the changes you suggested.

1 Like

hey try to use every() function .It will convert this to one line code only :smiley:

The every() method tests whether all elements in the array pass the test implemented by the provided function.