Ad
  • Custom User Avatar

    Looping once isn't definitely going to be better in terms of time than looping twice. You are still going to compare for both min and max in your loop, not to mention the overhead of min/max variables and conditional statements.

  • Custom User Avatar

    I believe the time complexity is still linear for both examples. The Math.max/min solution does have two loops but they are not nested. It would end up being O(2n) which would still condense down to O(n) like your solution. I think ultimately the one liner is more readable which would make it preferable. Please correct me if I'm missing something

  • Custom User Avatar

    This is an easy and readable code. Here there is no mention of writing the best code considering the Big O of the code running. So this can be a better option to do.

  • Custom User Avatar

    technically you have the more complex solution

  • Default User Avatar

    false, 0, "", null and undefined all have the property that when used in a if statement that they will equate to false. So it isn't necessary at all.

    As for production code, it is used without the === true. Which is generally done for all if statements. We also don't write code like

    if ((index === 4) === true) {
      // some logic
    }