Pattern

<!DOCTYPE html>

<html>

<head>

  <title>Array Operations</title>

</head>

<body>


  <button onclick="performOperations()">Perform Operations</button>


  <script>


  function printPattern(rows) {

  // Iterate through each row

  for (let i = 1; i <= rows; i++) {

    let pattern = '';


    // Add numbers based on the row number

    for (let j = 1; j <= i; j++) {

      pattern += i + ' ';

    //  pattern += j + ' ';

    }


    // Display the pattern for each row

    console.log(pattern);

  }

}


// To print the pattern with 3 rows


    let A = [12, 87, 34, 56, 34, 24, 56, 24, 65, 22];


    function getMax() {

      let max = A[0];

      for (let i = 1; i < A.length; i++) {

        if (A[i] > max) {

          max = A[i];

        }

      }

      return max;

    }


    function sortAscending() {

      let sortedArray = A.slice();

      for (let i = 0; i < sortedArray.length; i++) {

        for (let j = 0; j < sortedArray.length - 1 - i; j++) {

          if (sortedArray[j] > sortedArray[j + 1]) {

            let temp = sortedArray[j];

            sortedArray[j] = sortedArray[j + 1];

            sortedArray[j + 1] = temp;

          }

        }

      }

      return sortedArray;

    }


    function sortDescending() {

      let sortedArray = A.slice();

      for (let i = 0; i < sortedArray.length; i++) {

        for (let j = 0; j < sortedArray.length - 1 - i; j++) {

          if (sortedArray[j] < sortedArray[j + 1]) {

            let temp = sortedArray[j];

            sortedArray[j] = sortedArray[j + 1];

            sortedArray[j + 1] = temp;

          }

        }

      }

      return sortedArray;

    }


    function getSum() {

      let sum = 0;

      for (let i = 0; i < A.length; i++) {

        sum += A[i];

      }

      return sum;

    }


    function getIndex() {

      for (let i = 0; i < A.length; i++) {

        if (A[i] === 34) {

          return i;

        }

      }

      return -1;

    }

    

function Pattern(rows) {

  // Print the top part of the pattern

  for (let i = 1; i <= rows; i++) {

    let pattern = '';


    for (let j = 1; j <= i; j++) {

      pattern += j + ' ';

    }


    console.log(pattern);

  }


  // Print the bottom part of the pattern

  for (let i = rows - 1; i >= 1; i--) {

    let pattern = '';


    for (let j = 1; j <= i; j++) {

      pattern += j + ' ';

    }


    console.log(pattern);

  }

}


    


    function performOperations() {

      console.log("Maximum from the array:", getMax());

      console.log("Sorted in ascending order:", sortAscending());

      console.log("Sorted in descending order:", sortDescending());

      console.log("Sum of the whole array:", getSum());

      console.log("Index of value 34:", getIndex());

       console.log("pattern:",printPattern(3));

       console.log("pattern:",Pattern(3));

      

    }

  </script>


</body>

</html>

Comments

Popular posts from this blog

Bio Data Maker

10000+ Quotes and Status