Basic Algorithm with Typescript (Part 1)
basic programming for zero experience people
What is Algorithm ?
In mathematics and computer science field, algorithm is a finite list of instructions given to a computer or we can describe as a finite sequence of computer implementable instructions.
In simple explanation we can say algorithm is list of step to do something / to make something (output).
Every daily basis activities of your life are using the concept of algorithm, for example:
- How you go to school from your home ?
So, from the picture above we can write a code like this:
function goToSchool() {
wakeUp();
bath();
cook();
eat();
hopInSchoolBus();
arriveAndLearn();
}
Basic Logic Syntax in Programming you need to know:
- Operator
- Conditional
- Iteration
What is Operator ?
An Operator is a symbol / character that determine what action should be performed. There are several operator category that necessary for you:
- Arithmetic Operators
Arithmetic Operators are operators that perform mathematic calculations. For example, assume we have 2 variables that contain number value:
let a = 5;
let b = 10;// addition
console.log(a + b) // 15// substraction
console.log(a - b) // -5// multiplication
console.log(a * b) // 50// division
console.log(a / b) // 0.5// modulus
console.log(a % b) // 5// increment
a++;
console.log(a) // 6// decrement
b--;
console.log(b) // 9
💡 There are 2 categories of increment / decrement operators. Pre-increment means increment of the variable then save the expression value, on the other hand post-increment means increment of the variable then remember the original value before the increment process.
Pre increment:
let angka = 10;
let res = ++angka;console.log(res); // 11
Post increment:
let angka = 10;
let res = angka++;console.log(res) // 10
2. Relational Operators
Relational Operators are operators that evaluate what kind of relationships between 2 value. For example, assume we have 2 variables that contain number value same as number 1:
let a = 5;
let b = 10;// greater than
console.log(a > b) // false// lesser than
console.log(a < b) // true// greater than or equal to
console.log(a >= b) // false// lesser than or equal to
console.log(a <= b) // true// equality
console.log(a == b) // false// not equal
console.log(a != b) // true
3. Logical Operators
Logical operators are used to combine two or more relational operators. Logical operator will return a boolean value type.
let a = 5;
let b = 10;// AND
console.log(a > 5 && b > 10) // false// OR
console.log(a > 5 || b >= 10) // true// NOT
console.log(!(a > 5)) // true
4. Assignment Operators
Assignment operators are used for mathematical calculation and pass the result into a new variable.
let a = 5;
let b = 10;
let c = 0;// simple assignment
c = a + b // 15// add assignment
c += a // 5// substract assignment
c -= a // -5// multiply assignment
c *= a // 0// division assignment
c /= a // infinite
5. Miscellaneous Operators
- Negation operator
let x = 4;
let y = -x;console.log(y) // -4
- String concat operator
String concat operator used to combined 2 or more string variable
let message = "hello";
let text = " world";console.log(message + text + " again !");
// "hello world again !"
- Typeof operator
Typeof operator is used to return the data type of a variable
let num = 5;
console.log(typeof num) // number
This is the end of part 1, this article is more focusing on the concept of operators and how you apply it to your code. For the next part, I will write about conditional statement.