Set in Javascript.
Set is a collection specifically designed to hold unique values, meaning no duplicates are allowed within the Set.
In Set, we don’t have repeated values. If we try to add a duplicate value, the Set will only store one instance of it. Example:
let nums = new Set("bookkeeper");
console.log(nums);
//Set(6) {'b', 'o', 'k', 'e', 'p', 'r'}
We can also assign values to a Set using the ‘add’ method. Example:
let nums = new Set();
nums.add(3);
nums.add(4);
nums.add(3);
nums.add("Aisha");
nums.add("Aminah");
nums.add("Aliyah");
console.log(nums);
//Set(5) {3, 4, 'Aisha', 'Aminah', 'Aliyah'}
We can also iterate on a Set by using the ‘forEach’ loop. This allows us to execute a function for each element in the Set. Example:
let nums = new Set();
nums.add(3);
nums.add(4);
nums.add(3);
nums.add("Aisha");
nums.add("Aminah");
nums.add("Aliyah");
nums.forEach(value =>{
console.log(nums);
});
//3
//4
//Aisha
//Aminah
//Aliyah
We can likewise pass a value, the index value and the array itself through the .has() method. It is used with Sets to check if a specific element exists within the Set. Examples:
let nums = new Set();
nums.add(3);
nums.add(4);
nums.add(3);
nums.add("Aisha");
nums.add("Aminah");
nums.add("Aliyah");
console.log(nums.has("Aisha"));
//true
let nums = new Set();
nums.add(3);
nums.add(4);
nums.add(3);
nums.add("Aisha");
nums.add("Aminah");
nums.add("Aliyah");
console.log(nums.has(50));
//false
let nums = new Set();
nums.add(3);
nums.add(4);
nums.add(3);
nums.add("Aisha");
nums.add("Aminah");
nums.add("Aliyah");
console.log(nums.has(3));
//true
let nums = new Set();
nums.add(3);
nums.add(4);
nums.add(3);
nums.add("Aisha");
nums.add("Aminah");
nums.add("Aliyah");
console.log(nums.has("aminah"));
//false
Map in Javascript.
Map is a very powerful tool of storing data. It is a collection of key-value pairs, similar to objects.
A Map has a key and a value. However, unlike objects where keys must be strings or symbols, Maps allow keys to be any data type, including numbers, strings, objects, or even other Maps.
- Creating a Map. Example:
let map = new Map();
map.set("Aisha", "Javascript");
map.set("Aminah", "Java");
map.set("Aliyah", "PHP");
console.log(map.keys());
//[Map Iterator] {'Aisha', 'Aminah', 'Aliyah'}
The above is for getting the keys.
- We can also check if a particular key is available in the map. Example:
let map = new Map();
map.set("Aisha", "Javascript");
map.set("Aminah", "Java");
map.set("Aliyah", "PHP");
console.log(map.has("Aminah"));
//true
- For getting values;
let map = new Map();
map.set("Aisha", "Javascript");
map.set("Aminah", "Java");
map.set("Aliyah", "PHP");
console.log(map.get("Aisha"));
//Javascript
- For printing all the values;
let map = new Map();
map.set("Aisha", "Javascript");
map.set("Aminah", "Java");
map.set("Aliyah", "PHP");
for(let [k,v] of map){
console.log(k, " : ", v);
}
//Aisha : Javascript
//Aminah : Java
//Aliyah : PHP
- Adding additional values;
let map = new Map();
map.set("Aisha", "Javascript");
map.set("Aminah", "Java");
map.set("Aliyah", "PHP");
map.set("Aisha", "ML");
map.forEach((v,k) => {
console.log(k, " : ", v);
});
//Aisha : ML
//Aminah : Java
//Aliyah : PHP
Recursion in Javascript.
Recursion is a function in programming whereby a function calls itself within its own body. The resulting effect is that a loop is created with a modified input until a certain condition is met. Example:
function show(){
console.log("Hi")
}
show();
//Hi
In Recursion, the concept of "last in, first out" plays a vital role behind the scenes on how function calls are handled in memory. However, it doesn’t directly apply to the data being processed.
Recursions are very useful in instances such as having a JSON data or for calculating Factorial. It should however be noted that if the base case isn’t defined properly, the function can call itself infinitely, leading to a stack overflow error. So, it’s necessary to be cautious when dealing with larger outputs.
Factorial using Recursion.
Finding the factorial of a number (n!) involves multiplying it by the factorial of the previous number (n-1!). i.e. n! = n * (n-1) * (n-2) * (n-3)…(n-n). The value of 0! = 1. Thus, n! = n * (n-1)!
Finding the factorial of 5 or 5! = 5*4*3*2*1 = 120. 5! = 5*4! Recursion makes this process easier. Example of a Factorial using a loop;
function factorial(num) {
if (num === 0 || num === 1) {
return 1;
} else {
let result = 1;
for (let i = 2; i <= num; i++) {
result *= i;
}
return result;
}
}
const result = factorial(5);
console.log(result);
//120
Points to note:
- The Function also takes a number num as input.
- It checks for the base cases (0 and 1) and returns 1 if met.
- Otherwise, it initializes a variable result to 1.
- It uses a loop that iterates from 2 to num.
- In each iteration, result is multiplied by the current value of i.
- After the loop, result will hold the final factorial value.
function factorial(n){
if(n==0)
return 1;
else
return n*factorial(n-1);
}
let num = 4;
let result = factorial(num);
console.log(result);
//24
Points to note:
- Function Definition: Function factorial(n) { ... } defines a function named factorial that takes a number n as input.
- Base Case: if(n==0) return 1; checks if n is equal to 0. If so, it returns 1 because the factorial of 0 is 1. This is the base case that stops the recursion.
- Recursive Call: else return n*factorial(n-1); This part handles the non-base case scenario. It returns n multiplied by the result of calling factorial(n-1). This recursive call makes the problem smaller by calculating the factorial of n-1.
- Variable and Function Call: let num = 4; let result = factorial(num); defines a variable num with the value 4 and then calls the factorial function with num as the argument. The result of the function call is stored in the result variable.
- Output: console.log(result); prints the value stored in the result variable, which will be the factorial of 4 (24).
This approach is more efficient for larger factorials due to avoiding function call overhead.
Till Next Time. Ciao.