You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
7.4 KiB
JavaScript

console.log("Hello B322!");
// [SECTION] if, else if and else Statement
let numG = -1;
// if statement
// executes a statement if a specified condition is true
if(numG < 0) {
console.log('Hello');
}
/*
Syntax
if(condition){
statement
}
*/
// The result of the expression added in the if's condition must result to true, else, the statement inside if() will not run.
// You can also check the condition. The expression results to a boolean true because of the use of the less than operator.
console.log(numG < 0);
// results to true and so, the if statement was run.
numG = 0
if(numG < 0){
console.log('Hello again if numA is 0!')
};
// It will not run because the expression now results to false:
console.log(numG < 0);
// Another sample
let city = "New York"
if(city === "New York"){
console.log("Welcome to New York City!");
}
// else if Clause
/*
-Executes a statement if previous conditions are false and if the specified condition is true
- The "else if" clause is optional and can be added to capture additional conditions to change the flow of a program.
*/
let numH = 1;
if(numG < 0) {
console.log('Hello');
} else if (numH > 0) {
console.log('Hello from the other side');
}
// We were able to run the else if() statement after we evaluated that the if condition was failed.
// If the if() condition was passed and run, we will no longer evaluate to else if() and end the process there.
let numA = -1
if(numA > 0) {
console.log('Hello');
} else if (numH > 0) {
console.log('World');
}
//else if() statement was no longer run because the if statement was able to run, the evaluation of the whole statement stops there.
// Let's update the city variable and look at another example:
city = "Tokyo"
if(city === "New York"){
console.log("Welcome to New York City!")
} else if(city === "Tokyo"){
console.log("Welcome to Tokyo, Japan!")
};
// Since we failed the condition for the first if(), we went to the else if() and checked and instead passed that condition.
// let num = 10;
// if (num > 0) {
// if (num < 5){
// console.log("Number is between 0 and 5");
// }else {
// console.log("Number is greater than or equal to 5");
// }
// }else {
// console.log("Number is less than or equal to 0");
// }
// if (num > 0 && num < 5) {
// console.log("Number is between 0 and 5");
// }else if (num >= 5) {
// console.log("Number is greater than or equal to 5");
// }else{
// console.log("Number is less than or equal to 0");
// }
if(numA > 0) {
console.log('Hello');
} else if (numH === 0) {
console.log('World');
} else {
console.log('Again');
}
/*
Since both the preceding if and else if conditions failed, the else statement was run instead.
Else statements should only be added if there is a preceding if condition.
*/
// else {
// console.log("Will not run without an if");
// }
// else if (numH === 0) {
// console.log('World');
// } else {
// console.log('Again');
// }
// if, else if and else statements with functions
/*
- Most of the times we would like to use if, else if, and else statements with functions to control the flow of our application
- By including them inside functions, we can decide when certain conditions will be checked instead of executing statements when the JavaScript loads.
- The "return" statement can be utilized with condtional statements in combination with functions to change values to be used for other features of our application.
*/
let message = 'No message.';
console.log(message);
function determineTyphoonIntensity(windSpeed) {
if (windSpeed < 30) {
return 'Not a typhoon yet.';
}
else if (windSpeed <= 61) {
return 'Tropical depression detected.';
}
else if (windSpeed >= 62 && windSpeed <= 88) {
return 'Tropical storm detected.';
}
else if (windSpeed >= 89 || windSpeed <= 117) {
return 'Severe tropical storm detected.';
}
else {
return 'Typhoon detected.';
}
}
message = determineTyphoonIntensity(110);
console.log(message);
/*
- We can further control the flow of our program based on conditions and changing variables and results
- The initial value of "message" was "No message."
- Due to the conditional statements created in the function, we were able to reassign it's value and use it's new value to print a different output.
- console.warn() is a good way to print warnings in our console that cold help us developers act on certain output within our code
*/
if (message == 'Tropical storm detected.') {
console.warn(message);
}
// [SECTION] Truthy and Falsy
// -In Javascript a "truthy" value is a value that is considered true when ecnountered in a Boolean context
// - Values are considered true unless defined otherwise
// - Falsy values/exceptions for truthy:
/*
1. false
2. 0
3. -0
4. ""
5. null
6. undefined
7. NaN
*/
// Truthy Examples
// - If the result of an expression in a condition results to a truthy value, the condition retunrs true and the corresponding statements are executed
// - Expressions are any unit of code that can bve evaluated to a value
if (true) {
console.log('Truthy');
}
if(1) {
console.log('Truthy');
}
if ([]) {
console.log('Truthy');
}
// Falsy examples
if (false) {
console.log('Falsy');
}
if (0) {
console.log('Falsy');
}
if (undefined) {
console.log('Falsy');
}
// [SECTION] Conditional (Ternary) Operator
/*
- The Conditional (Ternary) Operator takes in three operands:
- 1. condition
2. expression to execute if the condition is truthy
3. expression to execute if the condition is falsy
- it can be used as an alternative to an "if else" statement
-Ternary operators have an implicit "return" statement meaning that without the return keyword, the resulting expressions can be stored in a variable.
- It is commonly used for single statement execution.
*/
// Single statement execution
let ternaryResult = (1 < 18) ? true : false;
console.log("Result of Ternary Operator " + ternaryResult);
// Multiple statement execution
/*Both functions perform two seperate tasks which changes the value of the "name" variable and returns the result storing it in the "legalAge"
*/
let name;
function isOflegalAge() {
name = 'John';
return 'You are of the legal age limit';
}
function isUnderAge() {
name = 'Jane';
return 'You are under the age limit';
}
let age = parseInt(prompt("What is your age?"));
console.log(age);
let legalAge = (age > 18) ? isOflegalAge() : isUnderAge();
console.log("Result of Ternary Operator in functions: " + legalAge + ', ' + name);
// [SECTION] Switch Statement
let day = prompt("What day of the week is it today?").toLowerCase();
console.log(day);
switch (day) {
case 'monday':
console.log("The color of the day is red");
break;
case 'tuesday':
console.log("The color of the day is orange");
break;
case 'wednesday':
console.log("The color of the day is yellow");
break;
case 'thursday':
console.log("The color of the day is green");
break;
case 'friday':
console.log("The color of the day is blue");
break;
case 'saturday':
console.log("The color of the day is indigo");
break;
case 'sunday':
console.log("The color of the day is violet");
break;
default:
console.log("Please input a valid day");
break;
}
// [SECTION] Try-Catch-Finally Statement
function showIntensityAlert(windSpeed) {
try {
alerat(determineTyphoonIntensity(windSpeed));
} catch (error) {
console.log(typeof error);
console.warn(error.message);
} finally {
alert('Intensity updates will show new alert.');
}
}
showIntensityAlert(56);