DSA in Javascript Programmes

(1) Write a Program to traverse the Elements in the Array.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      let data = [9, 45, 2, 8, 78, 0, 1, 41, 77];
      for (let i = 0; i < data.length; i++) {
        document.write(data[i] + "<br>");
      }
    </script>
  </head>
  <body></body>
</html>

image 2

(2) Insert Element in Array

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      let data = [9, 45, 2, 8, 78, 0, 1, 41, 77];
      let newE1 = 70;
      let position = 2;

      // Reverse Loop
      for (let i = data.length - 1; i >= 0; i--) {
        console.warn(data[i]);
        if (i >= position) {
          data[i + 1] = data[i];
          if (i === position) {
            data[i] = newE1;
          }
        }
        document.write(data)
      }
    </script>
  </head>
  <body></body>
</html>
image 3

(3) Delete Array in Javascript

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      let data = [9, 45, 2, 8, 78, 0, 1, 41, 77];
      let position = 3;

      // Delete Data From Loop
      for (let i = position; i < data.length; i++) {
        data[i] = data[i + 1];
      }
      document.write(data);
    </script>
  </head>
  <body></body>
</html>
image 4

(4) Linear Search in Javascript

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      let data = [9, 45, 2, 8, 78, 0, 1, 41, 77];
      let item = 45;
      let index = undefined;

      // Delete Data From Loop
      for (let i = 0; i < data.length - 1; i++) {
        if (data[i] === item) {
          index = i;
          break;
        }
        document.write(data);
        document.write("<br>");
        document.write(data.indexOf(item));
      }
    </script>
  </head>
  <body></body>
</html>
image 5

(5) Sum of Natural Numbers

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
    //Sum of Natural Numbers

function sumofNaturalNumber(num){
    let sum = 0;
    for (let i = 1; i <= num; i++) {
        sum = sum + i;
    }
    document.write(sum);
}

sumofNaturalNumber(5); 
    
    </script>
    </script>
  </head>
  <body></body>
</html>

Output: 15

Question: Find the Sum of the Digits:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      //Sum of Digits of a Number
      //when we are sure when the number of steps then we are going to use for loop but if we don't know the actual steps for the problem then we are going to use while loop.
      function sumOfDigits(num) {
        let sum = 0;
        while (num > 0) {
          sum += num % 10;
          num = Math.floor(num / 10);
        }
        return sum;
      }
      document.write(sumOfDigits(1287));
    </script>
  </head>
  <body></body>
</html>
image 6

Question: Find out the Fibonacci Number

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      //Find the Fibonacci Number of given Number

      function fib(n) {
        /*
            0 1 1 2 3 5 8
        */
        if (n < 2) {
          return n;
        }
        let prev = 0,
          curr = 1,
          next;
        for (let i = 2; i <= n; i++) {
          next = prev + curr;
          prev = curr;
          curr = next;
        }
        return next;
      }

      document.write(fib(6)); // Example usage
    </script>
  </head>
  <body></body>
</html>
image 7

Que: Missing Numbers Problem

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>

     //Given an Array nums containing n distinct numbers in the range [0,n], return the only number in the range that is missing from the array.

     //Example1

     //Input: nums= [3,0,1]
     //Output = 2
     //Explanation : n=3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums

    function MissingNum=(nums){
      //1+0+3=4  //1+2+3+0=6
      //First I Will find the sum of the all number 
      let num=0;
      for (let i=0;i<nums.length;i++){
        sum+= nums[i]
      }
      return nums.length * (nums.length+1)
    }
    </script>
  </head>
  <body></body>
</html>


//Array in Javascript 

Create Empty Array:

const arr = [];
const arr2 = new Array();
console.log(arr,arr2)
output: [1,2,3,4]

//Array contains same kind  of elements in the Array.

//How can You access the Elements from the Array.
const firstElement=arr[2]

//To find the length of the Array
const arrLength= arr.length;

//FindLastElement
const lastElement=arr[arrLength+1]
console.log(firstElement)

//What is the Time Compelxity of this Array?

The Timecomplexity of this code is (1) becuase we arr write just index number and fetching the elements from an array.

//How do you remove the Element from an Array?
const lastElement=arr.pop()
console.log(arr,lastElement)

//How do you add eleemnt at the end of array?
arr.push(5)
console.log(arr)

//How do you add an element to the start of an array?

arr.unshift(0) //
console.log(arr)

//How can you find elements from an array?
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script>
      // How do you check if an Element Exist in Array?

      function findElement(arr, target) {
        for (let x of arr) {
          if (x == target) {
            return true;
          }
        }
        return false;
      }

      // Define the array
      let arr = ["Hello", "World", "How", "Are", "You"];

      // Call the function with the array
      console.log(findElement(arr, "Hello"));
      console.log(findElement(arr, "H"));
    </script>
  </head>
  <body></body>
</html>


Binary Search in Javascript
 // String Methods: Match(), Search(),includes()

      //Binary Search in Javascript

      //If array is sorted then it will take a o(n) Time complexity.

      //But if the array has 1000 inputs then it will take 1000(n) of time

      //Suppose we have following array

      arr=[1,2,3,4,5,6,7,8,9]

      //In Binary search we will find the Middle point.

      midpoint=5;

     // n -> n/2 -> n/4 (This will become time complexity according to the condition)

     // N = 2^K
     //log(base2)N = K
image 8

Objects in Javascript

image 9

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *