JavaScript Conditionals

If

var x = 3 if (x < 3) { // Code here }

- The code within the block (between the curly braces) will only run if the condition is met, otherwise it will be ignored

- Whether or not the condition is met, any code after the block will be run

Alternative Conditionals

var x = 3 if (x > 3) { // Code here } else if (x < 0) { // Code for alternative condition } else { // Code to be executed if none of the specified conditions are met }

- You can have as many "else if" conditions as you want (or none)

Types of Conditions

true !false // ! means "not" 3 == 3 // == checks if values are equal 3 != 4 // != checks if not equal 3 < 4 // < checks if value on left is less than value on right 3 > 2 // > checks if value on left is greater than value on right 3 <= 4 // <= means less than or equal to 3 >= 2 // >= means greater than or equal to !(3 == 4) // !() will negate the value within parenthesis 3 == 9 || 3 < 7 // || means OR, true if the conditions on EITHER side are true 3 > 2 && 3 < 5 // && means AND, true if the conditions on BOTH sides of it are true 3 > 2 && (3 > 5 || 6 > 2 || 1 < 4) // Complex combinations can be made

- All of the above examples have the value of true

Challenge

Write a script that stores GPA and entrance exam score as variables. If the GPA is higher than 3.5 and the test score is higher than 85, display to the console "Application accepted", otherwise display "Application rejected". If only one of the conditions is met, display a message indicating which one failed.
As a bonus, make a web page that has input fields and a submit button, and have it display the result on the page when the button is clicked.

Completed