technical
<script>
// https://www.hellojavascript.info/docs/general-javascript-questions/javascript-fundamentals/arrow-functions
// Redux is a global state management, it is used to read data from redux store and to read data we dispatch action to the store.
// Redux is a pure function
// redux is used when we want to deal with large projects, with large number of states. then we use redux.
// Action -> Reducer -> Store
// Reducer is a function which takes initial state and action and will return the updated state.
// == operator is used to check the value.
// === operator is used to check the value as well as the data type
// Polymorphism means the same function with different signatures is called many times
. In real life, for example, a boy at the same time may be a student, a class monitor,
etc. So a boy can perform different operations at the same time. This is called
polymorphism.
// Different Kinds of Loops -- JavaScript supports different kinds of loops:
// for - loops through a block of code a number of times
// for/in - loops through the properties of an object --const numbers = [45, 4, 9, 16, 25]; for (let x in numbers) { txt += numbers[x]; }
// for/of - loops through the values of an iterable object -- let language = "JavaScript"; for (let x of language) { text += x + "<br>"; }
// while - loops through a block of code while a specified condition is true
// do/while - also loops through a block of code while a specified condition is true
// Differences between forEach() and map() methods
// The forEach() method does not returns a new array based on the given array.
// M- The map() method returns an entirely new array.
// The forEach() method returns “undefined“.
// M- The map() method returns the newly created array according to the provided callback function.
// The forEach() method doesn’t return anything hence the method chaining technique cannot be applied here.
// M -With the map() method, we can chain other methods like, reduce(),sort() etc.
// It is not executed for empty elements.
// M- It does not change the original array.
// Lifecycle methods = mounting-> updating -> unmounting
// the arrow functions, it doesn't use function and return keyword whereas a normal function uses.
// Arrow functions are not constructible as it doesn't use new keyword whereas normal functions can be.
// iterators in javascript
// Composition refers to the practice of creating reusable UI components by combining smaller components together. This allows developers to break down complex UIs into smaller, more manageable parts, which can be easily reused across different parts of the application.
// function which gets immediately called after the function definition without being called so that is nothing but self invoking function.
// Usecallback Hook is used to avoid unnecessary rendering and it is used to return a memoized function.
// JSX stands for javascript XML. It helps us to write HTML in React.
// React's Strict mode helps developers identify and fix issues in their code. Strict mode runs in development mode and can be enabled by adding a component at the beginning of the application. It checks for potential problems in the code, such as possible memory leaks, and warns the user about their presence.
// React Fragments allows us to group list of childern, without adding extra nodes.
// Error Boundaries are mainly used to catch the error in the child component. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
// Hoisting allows us to use the variable before its declaration
// Event Loop is mainly used for handling asynchronous operatons. Process is Stack -> Web API -> Event Queue.
// Promises, it allows us to handle the asynchronous operations so when it is useful when we want to manage the condition where we want to wait for the outcome or the result of an operation.
// Promises has stages rejected fulfilled and pending.
// Whne Promise is rejected, it returns error object.
// When Promise is fulfilled, it returns a value.
// When Promise is pending, it returns a undefined.
// shift() removes the first item of an array
// pop() removes the last item of an array,
// arra.splice(-1) removes the last item of an array,
// arra.splice(-2) removes the last two item of an array ;
// Status Codes
// 102 - the server has accepted the complete request but has not yet completed it
//- 200 - OK request success
// 202 - the request has been accepted for processing, but the processing has not been finished yet
// 300 - This Status Code indicates that the request has more than one possible response
// 302 - a redirection message that occurs when a resource or page you're attempting to load has been temporarily moved to a different location
//- 400 - Bad request. ...
//- 404 - Resource not found. ...
//- 500 - Internal server error.
// API call methods
// GET, POST, PUT, PATCH, DELETE
// 01) get only common values from array
// Find duplicates in an array using javaScript
const arry = [1, 2, 1, 3, 4, 3, 5];
let tofindDuplicates = (arry) =>
arry.filter((item, index) =>
arry.indexOf(item) !== index
);
console.log(tofindDuplicates(arry));
// 02) get matching vovels
// defining vowels
const vowels = ["a", "e", "i", "o", "u"];
function countVowel(str) {
// initialize count
let count = 0;
// loop through string to test if each character is a vowel
for (let letter of str.toLowerCase()) {
if (vowels.includes(letter)) {
count++;
}
}
// return number of vowels
return count;
}
console.log(countVowel("elephant"));
let abcstr = ""
const countVowels = str => Array.from(str)
.filter(letter => 'aeiou'.includes(letter)).length;
console.log("abcstr>>>", countVowels("elephant"))
// 03) print key as value and value as key in array
var obj = {"a":1, "b": 2, "c":3};
let outArray = {};
Object.entries(obj).map( ([key, value], index) =>
outArray[obj[key]] = key
)
console.log("outArray>>>>", outArray);
// 04) reverse string
// 05) Fibonacci Series
let n1 = 0, n2 = 1, n3;
n3 = n1+n2;
while ( n3 < 10 ){
console.log( "Fibonacci", n3 );
n1 = n2;
n2 = n3;
n3 = n1 + n2;
}
// 06) palendrome
var palendromeString = "heleh";
let palendromeStringArray = palendromeString.split("");
let revString = "";
for ( let i = palendromeStringArray.length-1; i >= 0 ; i-- ){
revString += palendromeStringArray[i];
}
if ( revString == palendromeString ){
console.log("This is a palendrome String >>>>", revString);
} else {
console.log("This is not a palendrome String >>>>", revString);
}
// 07) map function vs for each
// 08) json filter example
var jsonarray = [
{"name":"Lenovo Thinkpad 41A4298","website":"google222"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"}
];
let newdata = jsonarray.filter( (item,index) =>
item.website == "google"
)
console.log(newdata);
// 09) multidimentional array to array
const multiarray = [1,2,3,4,5, [6, 7,8,9,8,7], 6,7,8,9];
let outstring = "";
function makeSingleAray( arr ) {
for ( let i = 0; i < arr.length; i++ ){
if ( Array.isArray(arr[i]) ){
makeSingleAray(arr[i])
} else {
outstring += arr[i]
}
}
return outstring;
}
console.log( "multiarray>>>>", makeSingleAray(multiarray).split("").sort().join(",") );
// 10) largest number with loop in array
function largestElement(arr) {
let largestNum = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > largestNum) {
largestNum = arr[i];
}
}
return largestNum;
}
const num1 = [10, 15, 18, 20, 1, 23, 16, 15, 2];
const res = largestElement(num1);
console.log("The largest element in the array is:" + res);
// 11) smallest number with loop
function smallestElement(arr) {
let smallNumber = arr[0];
for ( let i= 1; i < arr.length; i++ ){
if ( arr[i] < smallNumber ){
smallNumber = arr[i]
}
}
return smallNumber
}
console.log("The smallest element in the array is:" + smallestElement(num1))
// 12) remove value from array
let testarr = [5, 6, 7, 8, 9];
var newarr = testarr.filter((item) => item !== 5);
console.log(newarr);
// 13) Sort array without sort()
function mysort(arry) {
let len = arry.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
if (arry[j] > arry[j + 1]) {
let temp = arry[j];
arry[j] = arry[j + 1];
arry[j + 1] = temp;
}
}
}
return arry;
}
console.log("unsortedArray >>>", mysort(unsortedArray));
</script>
Comments
Post a Comment