Skip to content

Instantly share code, notes, and snippets.

@tripdog
Last active June 12, 2021 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripdog/3ee51e456b5a8f136303709434c7304d to your computer and use it in GitHub Desktop.
Save tripdog/3ee51e456b5a8f136303709434c7304d to your computer and use it in GitHub Desktop.
Conditionals (if, if else)

Conditionals

If else

const lifePhase = (age) => {
    if (age <= 3) {
        return 'baby'
    } else if (age > 3 && age <= 12) {
        return 'child'
    } else if (age > 12 && age <= 19) {
        return 'teen'
    } else if (age > 19 && age <= 64) {
        return 'adult'
    } else if (age > 64 && age <= 140) {
        return 'senior citizen'
    } else { return 'This is not a valid age' }
}
console.log(lifePhase(141))

Compare 3 numbers to see which one returns the highest value or a tie

const maxNum = (num1, num2, num3) => {
    if (num1 > num2 && num1 > num3) {
        return num1       
    } if (num2 > num1 && num2 > num3) {
        return num2
    }  if (num3 > num2 && num3 > num1) {
        return num3
   }else { return "It's a tie!" }
}

Check to see if a number is divisible by 5

const num = 5
if (num > 0) {
    console.log('positive')
} if (num % 5 === 0) {
    console.log('divisible')
}else{ console.log('none')}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment