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)



No comments:

Post a Comment

Java Script

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