Basic Algorithm with Typescript (Part 3)
Basic knowledge of iteration or loop and array in programming
Iteration or Loop
What is iteration or loop anyway ? Iteration in programming means repeating steps or instructions in one time or more than one times until a particular condition is true. Usually iteration statement used to do some process with array data type variable.
Array
What is array ? array is a variable that consist of multiple value either they are String, Boolean, Number, Date or any, for example I’ll show you how to write array in your code.
// array of number
let array = [1, 2 ,3, 4, 5];// array of string
let array = ["mango", "apple", "kiwi"];// array of boolean
let array = [true, false, true, true];// array of any
let array = ["test", 1, 1.5, true, new Date()];
When you initialize an array variable you must open it and close it with a square bracket symbol ([ ]) and splitting each element by a comma symbol (,).
Basic Syntax
There are several syntax when using iteration that you need to know:
- For loop: for-loop is the most basic syntax when using iteration statement, it consist 3 sections which splitted by semicolon symbol (;)
let n = 5;
for (let i=0; i<n; i++) {
console.log(i) // will be printed 5 times from 0 - 4
}
The first section (let i=0) used to define where the index position you want to start, the second section (i<n) used to define how many times the iteration process will be executed and the last section (i++) is to define the increment process of loop (you can use assignment operator in here) for example:
//this all will be worked
i++;
i--;
i*=2;
i+=2;
i/=2;and so on...
- While: while statement is executed as long as given statement condition is true. it is used when you don’t know how many times the number of your iteration will be executed, which means it is based only on conditionals.
while(condition) { // relational operator or logical operator
// do something here
}
for example this is a while statement below
let isGameRunning = true;while(isGameRunning) {
if (player.score == 100) {
isGameRunning = false;
break;
}
}
let index = 0;while(index < 10) {
// will be printed 10 times from 0 - 9
console.log(index);
index++;
}
- do while: do while statement is similliar with while statement but do while runs first then checking the conditional, for example I’ll show you this code below
let index = 10;do {
console.log(index) // this will be printed (runs only 1)
} while(index < 10)while(index < 10){
console.log(index) // this will never run
}
while vs do while
- while checks the conditionals before entering the loop, on the other hand do while checks the conditionals when exiting the loop.
- while may run zero times however do while may run at least once.
How to accessing a specific element in array ?
Accessing a specific element in array is a quite simple, you can pass an index variable in your iteration (for-loop or while or do while) inside a square bracket symbol ([]), for example I’ll show you this code below
let array = ["mango", "apple", "carrot"];// accessing item 0 element
console.log(array[0]) // "mango"for (let i=0; i<array.length; i++) {
console.log(array[i]); // accessing each element
}
Javascript array built in method
what is array built in method ? array built in method is a function which does some low level of abstraction process such as finding an element in array, reversing an array, get a length of array, mapping an array and so on.
I’ll give you several example that necessary for you to know about array built in method.
- Array.map(function()): formatting an array and return it to the new array variable.
let arr = [1, 2, 3, 4, 5]let newArr = arr.map((item) => {
return item * 2;
});console.log(newArr) // [2, 4, 6, 8, 10]
2. Array.filter(function()): filtering an array with some condition and return it to the new array variable.
let arr = [1, 2, 3, 4, 5];let newArr = arr.filter((item) => {
return item % 2 == 0;
});console.log(newArr) // [2, 4]
3. Array.reduce(function()): return a single result of variable (string, number or boolean) which is the function accumulated result.
let numbers = [1, 2, 3, 4, 5]let sum = numbers.reduce((total, item) => {
return total+= item;
}, 0);console.log(sum) // 15
4. Array.find(function()): searching a value and will return the value to a new variable if it was found, otherwise will return an undefined if it wasn’t found.
let arr = ["mango", "apple", "carrot"];let foundedItem = arr.find((item) => {
return item == "apple";
});let foundedItem1 = arr.find((item) => {
return item == "orange";
});console.log(foundedItem) // "apple"
console.log(foundedItem1) // undefined
5. Array.push(value): inserting new element in an array
let arr = [3, 2, 6, 8, 1];
arr.push(5);console.log(arr) // [3, 2, 6, 8, 1, 5]
6. Array.pop(): remove the last element of array and return it.
let arr = [1, 2, 3, 4, 5];
let lastEl = arr.pop();console.log(arr) // [1, 2, 3, 4]
console.log(lastEl) // 5
7. Array.includes(value): checking if an array contains a value that we passed to the parameter (return a boolean value)
let arr = [1, 2, 3, 4, 5];
let isExists = arr.includes(8);console.log(isExists) // false
This is the end for all article before, this article is more focusing on the concept of iteration or looping and how you apply it to your code. for the rest of array built in method you can check it at https://www.w3schools.com/jsref/jsref_obj_array.asp
cheers 😃 🍺