Sunday, February 25, 2024

Java Script

 1 Feb 2024

(1) Find the Second largest number in the array: -

const findSecondLargest=(a)=>{

    var largestNumber = 0;

    var secondLargestNumber = 0;

        for(var i = 0; i <= secondLargestNumber.length-1; i++){

            if(a[i] > largestNumber){

              secondLargestNumber = largestNumber;

                largestNumber = a[i];

            }else if(a[i] > secondLargestNumber && a[i] !== largestNumber){

                secondLargestNumber = a[i]

            }

    }

    console.log("Hello i am the largest===>>",largestNumber)

    console.log("Hello i am the secondlargest===>>",secondLargestNumber)

}

findSecondLargest(demoArray)


(2) Write a Java Script Program to reverse the string: -

Answer: - To reverse a string first of all we need to convert a string into an array, so we use the split function in javascript for converting a string into an array of alphabets.

There are many ways to solve this problem, but my easy logic is to pop the item from the array and push it into another array, the pop method removes the last index value from the array and stores it in a variable, and after that, we push the same variable in the another array. push method pushes the item into the array at last. in that, we get an array that is the reverse of the first array.

after that we have to join the array to get the string so we use join(") and after that, we get a simple reversal of the string.

 const reverseString=(a)=>{

      a = a.split('')

      var result = []

     while(a.length > 0){

         var temp = a.pop()

         result.push(temp)

     }

     result = result.join('')

     console.log(result)

 }

 var name = "Mohan"

 reverseString(name)



Wednesday, January 31, 2024

Date: - 31 January 2024

JavaScript Interview Programming Questions: -
 (1) write a program to take an array input and return the two arrays one of unique numbers and one for duplicate numbers: -

const findDuplicateNumber=(a)=>{
    const duplicateObject = {};
    const uniqueArray = []
    const duplicateArray = []
    for(let numbers of a){
     duplicateObject[numbers] = (duplicateObject[numbers] || 0) + 1
    }
    for(let keys in duplicateObject){
        if(duplicateObject[keys] > 1){
            duplicateArray.push(keys)
        }else{
            uniqueArray.push(keys)
        }
    }
    return {duplicateArray, uniqueArray}
}
testingArray=[2,4,5,55,16,22,55,16,5]
const duplicateNumber = findDuplicateNumber(testingArray)
console.log(duplicateNumber.duplicateArray)
console.log(duplicateNumber.uniqueArray)


(2) Write a program to find if a string is an anagram or not?


const isAnagram=(name1, name2)=>{
    if(name1.length != name2.length){
        console.log(name1,"and",name2, "can not be Anagram")
    }else{
        let counter = {}
        for(let numbers of name1){
            counter[numbers] = (counter[numbers] || 0) + 1
        }
        for(let numbers2 of name2){
            if(!counter[numbers2]){
                console.log("this can not be anagram===")
                return 
            }
            counter[numbers2] -= 1
        }
        console.log("This both strings are anagram")
    }

}
const name1 = "jokar";
const name2 = "okelr";
isAnagram(name1,name2);



Friday, July 8, 2022

Array Data Structure Using Java Script

 Array Data Structure Using Java Script : -


Array is a Data Structure which contain a fix number of items and these items should be of the same type. 

Basically there are Two things in Array,

1. Element - Each and Every data which is store in array called element.

2. Index - The location of element is called index. we identify the element with the help of index and index is always a numeric value.

Operations we performed on Array:- 
Traversing - visiting each and every element of array at least once is called traversing,  we can travers the array in using Java Script as well.

example - 

let traversingArray = [21,75,98,45,11,16]

for(var i = 0 ; i < traversingArray.length; i ++){
    console.log("Index",i +" value "+traversingArray[i])
}

output

Index 0 value 21
Index 1 value 75
Index 2 value 98
Index 3 value 45
Index 4 value 11
Index 5 value 16


Insertion - Enter a new element in array either in start or in end or we can enter the element on any position of array.

//Replacing the first index or any given index :- 
//Enter 25  in the starting of given array [0] index:- 

function insertElement(){
var insertionArray = [10,4,6,18,20]
for(var i = 0 ; i < insertionArray.length ; i++){
    if(i == 0){
        insertionArray[0] = 25
    }
}
console.log(insertionArray)
}

insertElement()

OutPut :- 

[ 25, 4, 6, 18, 20 ]

//Enter element in the starting of Array: -

//Enter 25  in the starting of given array [0] index:- 

function insertElement(){
    var pos = 0
var insertionArray = [10,4,6,18,20]
for(var i=insertionArray.length -1;i>= 0;i--){
   insertionArray[i+1] = insertionArray[i];
   insertionArray[i] = 25;
}
console.log(insertionArray);
}

insertElement();


output: -

[25,10,4,6,18,20]


//Enter 25 at the end of array:- 

function insertElement(){  
var insertionArray = [10,4,6,18,20]
insertionArray[insertionArray.length] = 25
 console.log(insertionArray);
 }

insertElement();

output: -

[10,4,6,18,20,25]

//Enter a new element in the middle or any where in array:- 

function insertionElement(){
   var pos = 5
   var x = 30
    var implementedArray = [12,34,54,44,76,56]
    for(var i = implementedArray.length -1; i >= pos -1; i-- ){
         implementedArray[i] = implementedArray[i - 1];
         implementedArray[2] = 30;
    }
    console.log(implementedArray)
}
insertionElement()

output:- 
[ 12, 34, 30, 44, 44, 76 ]


Deletion - Deleting a element from the array is called deletion, we can delete the item from the present array.

//Remvoe 32 From the array : -

function removeElement(){
    var removalArray = [23,43,65,32,76]
    for(var i = 0; i<= removalArray.length - 1; i ++){
       removalArray[i] = removalArray[i+1]
    }
    console.log("hii value",removalArray)
}
removeElement()

OutPut :- 
hii value [ 43, 65, 32, 76]

Search - Searching an element in array by value or by index is called searching.
// Search 52 in the given array: -

function searchElement(){
    var searchArray = [23,43,65,32,76,52]
    for(var i = 0; i<= searchArray.length - 1; i ++){
       if(searchArray[i] == 52){
           console.log(" we have found the value",searchArray[i])
       }
    }
}
searchElement()

OutPut : - 
we have found the value 52

Updated -  we can update the value of index in array.

//update the value 52 with 99
//Remvoe 52 in the array : -

function updateElement(){
    var updateArray = [23,43,65,32,76,52]
    for(var i = 0; i<= updateArray.length; i ++){
       if(updateArray[i] == 52){
          updateArray[i] = 99
       }
    }
    console.log(updateArray)
}
updateElement()

OutPut : -

[ 23, 43, 65, 32, 76, 99 ]

These are the operations we can performed on array using java script or any new programming language.
we see all these operations using Program in java Script.










Thursday, April 28, 2022

Higher Order Components

Higher Order Components in React-Native


A Higher order component is a component that take a component as a input and returns a new component.


A higher order component  is react technique for reuse component logic.


HOCs basically incorporate the don’t-repeat-yourself (DRY) principle of programming, which you’ve most likely come across at some point in your career as a software developer. It is one of the best-known principles of software development, and observing it is very important when building an application or writing code in general.


The most interesting part of using higher-order functions is composition. We can write small functions that handle one piece of logic. Then, we can compose complex functions by using the different small functions we have created. This reduces bugs in our code base and makes our code much easier to read and understand.


import React from 'react';

// Take in a component as argument WrappedComponent

const higherOrderComponent = (WrappedComponent) => {

// And return another component

  class HOC extends React.Component {

    render() {

      return <WrappedComponent />;

    }

  }

  return HOC;

};

Java Script

  1 Feb 2024 (1) Find the Second largest number in the array: - const findSecondLargest=(a)=>{     var largestNumber = 0;     var secondL...