Javascript Style
use the left and right arrow keys to navigate
by Paula Bannerman dcartist.com
One thing to remember
It's not that bad
It just seems bad
You actually do conditionals (if/else) in your life every day.
"If Else" statements are like giving choices (based on specific information) to someone who hasn't been exposed to any of it before. The only difference is you giving the choices to a computer, not a human. So they will not understand what you "mean" to say. You have to be literal.
Let's just say you have a light switch set
to off
let light = "off"
You tell someone to turn the light on it if the light is off it
would look like this:
let light = "off"
if (light == "off"){
light = "on"}
Now you can tell them if it's off turn it on
or if it's on turn it off:
let light = "on"
if (light == "off"){
light = "on"}
else{
light = "off"}
Let's look at the some of the Basic logic conditions
if ("this condition is true"){
"do something"}
else if ("this other condition is true"){
"do something else "}
else{
"do this since nothing above is true"}
if ("this condition is true"){
"do something"}
else if ("this other condition is true"){
"do something else "}
else{
"do this since nothing above is true"}
you start with an if and a condition you want to check
if ("this condition is true"){
"do something"}
else if ("this other condition is true"){
"do something else "}
else{
"do this since nothing above is true"}
if it's true then do something
if ("this condition is true"){
"do something"}
else if ("this other condition is true"){
"do something else "}
else{
"do this since nothing above is true"}
else if something else is true do something else
if ("this condition is true"){
"do something"}
else if ("this other condition is true"){
"do something else "}
else{
"do this since nothing above is true"}
When nothing in the conditional came out to be true you can use else as a catch all.
When you need an if else inside another if statement, then you will where that condition is needed. Like adding a container inside another container.
if ("this condition is true") {
do something}
else if ("this other condition is true")
{
if ("this condition is true"){
do something}
else if ("this other condition is true"){
"do something else "}
else{
"do this since nothing above is true"}
}
else{
"do this since nothing above is true"}
let broke = false
let outside_temp = 75
if (broke == true){
console.log("FREE office coffee")}
else if (outside_temp <= 60){
console.log("Hot Coffee from Starbucks")}
else{
console.log("Starbucks Ice Coffee") }
First we find something to compare, which in this case is being broke and the weather
let broke = false
let outside_temp = 75
if (broke == true){
console.log("FREE office coffee")}
else if (outside_temp <= 60){
console.log("Hot Coffee from Starbucks")}
else{
console.log("Starbucks Ice Coffee") }
If you are broke, then you are drinking FREE office coffee
let broke = false
let outside_temp = 75
if (broke == true){
console.log("FREE office coffee")}
else if (outside_temp <= 60){
console.log("Hot Coffee from Starbucks")}
else{
console.log("Starbucks Ice Coffee") }
else if (elif) it's cold outside then you're drinking Hot Coffee from Starbucks
let broke = false
let outside_temp = 75
if (broke == true){
console.log("FREE office coffee")}
else if (outside_temp <= 60){
console.log("Hot Coffee from Starbucks")}
else{
console.log("Starbucks Ice Coffee") }
else then you're drinking Ice Coffee from Starbucks
Think logically
Think in simple terms
If something is true then do this...
Else If something other than the first statement is true then do this...
Else just do this and call it a day...
Indentation isn't important
but your what you put inbetween {} is important
just remember you can do tons with if/else
keep practicing using them