Skip to content

Instantly share code, notes, and snippets.

@hossainlab
Last active May 30, 2019 15:17
Show Gist options
  • Save hossainlab/50f090cbcd7df593701428821c198db4 to your computer and use it in GitHub Desktop.
Save hossainlab/50f090cbcd7df593701428821c198db4 to your computer and use it in GitHub Desktop.
// ! Comparison Operators and Conditonal Logic
let n = 7;
if (n <10) {
console.log("n is smaller than 10");
} else if(n < 20) {
console.log("n is smaller than 20");
} else {
console.log("n is greater than 10");
}
// Comparison between m and n
let m = 25;
let n = 22;
if (m>n) {
console.log("m is gretaer than n");
} else if (m==n) {
console.log("m is equal to n");
} else {
console.log("m is less than n");
}
let x = 10;
// This is an important concept in JS
// Here, 8 is newly assigned value, so output is x = 8.But let x = 10
// Cause of reassign the value of x
// So We Should follow a convention in Conditional Logic
// Always a scaler value should be compare like this (8==x)
// If We follow this convention, we may get an error
// Code Here
if (x = 8) {
console.log("x is equal to 8");
} else {
console.log("x is not equal to 8");
}
console.log(x)
// ! The best way
let y = 10;
if (8 == y) {
console.log("y is equal to 8");
} else {
console.log("y is not equal to 8");
}
console.log(y);
// Nested Condition
let personOne = "Adib";
let personTwo = "Abir";
// let areTheyBrother = true;
let areTheyBrother = false;
if ("Adib" == personOne) {
if ("Abir" == personTwo) {
if (areTheyBrother) {
console.log("Abir and Adib are brother");
} else {
console.log("The are not brother");
}
}
}
// Group Condition Using &&(and) Operator
let personOne = "Adib";
let personTwo = "Abir";
// let areTheyBrother = false;
let areTheyBrother = true;
if ("Adib" == personOne && "Abir" == personTwo && areTheyBrother) {
console.log("They are brother");
} else {
console.log("They are not brother");
}
// Another Nested Condition Using ||(or) Operator
let n = 6;
// 2 3 5 7
if (n <10) {
if (2 == n || 3 == n || 5 == n || 7 == n ) {
console.log(n +" is smaller than 10 and it's a prime number");
} else {
console.log(n +" is smaller than 10 and it's not a prime number");
}
} else {
console.log(n+" is greater than 10");
}
// Gorup Conditon Using &&, ||
let n = 12;
// 2 3 5 7
if ( n <10 && (2 == n || 3 == n || 5 == n || 7 == n )) {
console.log(n +" is smaller than 10 and it's a prime number");
} else if (n < 10) {
console.log(n +" is smaller than 10 and it's not a prime number");
} else {
console.log(n+" is greater than 10");
}
// Negative or Positive
let i = -10;
if (i > 0) {
var result = (i+" is a positive number");
} else {
var result = (i+" is a negative number");
}
console.log(result);
// ! Odd or Even
let n = 17;
if (n %2 == 0) {
console.log("This number",n,"is even");
} else {
console.log("This number",n,"is odd");
}
// Another Way To Check Odd or Even
// TODO: I have to read template literals in JS
let n = 7;
let remainder = n%2;
if (0 == remainder) {
console.log(`${n} is an even number`);
} else {
console.log(`${n} is a odd number`);
}
// Ternary Operator
// ODD or EVEN
let i = 11;
let result;
let remainder = i%2
result = (0 == remainder) ? "Even": "Odd";
console.log("This number is",result);
// // Negative Positive
result = (i > 0) ? "Positive":"Negative";
console.log("This number is",result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment