Hacks

De Morgan’s Law

Using De Morgan’s Law you can simplify the following,

!(A && B) === (!A || !B)

!(A || B) === (!A && !B)

Examples:

Without De Morgan’s Law: if (!(a || b)) {}

  • Confusing to read and understand

Using De Morgan’s Law: if (!a && !b){}

  • Simplified, easy to read and understand

Without De Morgan’s Law: if (!(a && b)) {}

With De Morgan’s Law: if (!a || !b) {}

Without De Morgan’s Law:

function check() {
   if (!(a || b || c)) {
    return false; 
   } 
    return true; } 

This function returns false only if all are false. If at least one is true then the function returns true.

Using De Morgan’s Law:

function checkC() {
    return a || b || c; 
    
}

This function returns if at least one is true. Using De Morgan’s Law we can write the same code in a shorter simpler way that makes it easier to read and comprehend.

Purpose

De Morgan’s Law allows further simplification of logical expressions involving And(&&), Or(   ), and Not(!). This allows code to be simplified making it easier to read and understand. Using this law enhances clearer logic. Furthermore, by minimizing the use of nested conditions, these laws contribute to error prevention and enhance the overall manageability of the code.

Homework Hacks

1. Password Creator Hack

This hack is a password creator that checks whether a user’s password meets certain criteria (length, character types, etc). Use what you learned with relational operators to create a simple password create and use De Morgan’s Law to simplfiy the conditions for easier readability and understanding

Features:

  • The password must be at least 10 characters long.
  • It must include both uppercase and lowercase letters.
  • It must contain at least one number.
  • It must not contain any spaces.

Extra Features:

  • The password must contain at least 1 special character
  • The password contains no more than 3 of the same letter in a row

2. Personality Quiz Hack

Create a simple multiple-choice personality quiz that gives you a brief description of your personality based on the questions answered from the quiz. Use what you learned from relational operators and apply it for this hack.

Requirements:

  • Must have at least 10 differnt questions
  • Must be multiple choice
  • Must have multiple different outputs based on results

Popcorn Hacks

1. Outfit Picker Based on Weather

Create an outfit suggester based on the weather by using relational and logical operators. Each range of temperature should suggest what to wear based on the temperature.

Requirements

  • Must have at least 4 unique outputs
  • Use both relational and logical operators
  • Allow the use to input the temperature

2. Which number is bigger?

Use what you learned from relational operators to identify which number is bigger from a list of numbers, and order them from least to greatest.

3. Even or Odd?

Use logical operators to identify if the inputed number is even or odd.