Must do pattern questions : Part-1 [ Javascript]

Jagroop Singh - Apr 9 '23 - - Dev Community

The main motive of learning programming is to gain the ability to instruct computers to perform tasks and solve problems.It can be anything building some software that sell something, creating games, personal portfolio's, blog website like DEV.to and lot's and lot's more.

But for reaching that far the first step of each part is having ability of Logical and Analytical Reasoning.Pattern-based questions develop the logical and analytical reasoning skills. They often involve identifying and analysing the structure of a pattern, and then using that understanding to generate a solution. This helps in developing problem-solving skills, which are essential in DSA.

So today In this part I am sharing 11 patterns that a beginner can start's in JavaScript. After that in second part I will share some more complex pattern problems and solutions.

// Pattern  : 1
// *****
// *****
// *****
// *****
// *****

// This function will print the Pattern 1 that is described above
const printSquare = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = 1; j <= input; j++) {
      patternCapturer += "* ";
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
printSquare(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern 2 :
// *
// **
// ***
// ****
// *****

// This function will print Pattern 2 as described above
const printRightAngledTriangle = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = 1; j <= i; j++) {
      patternCapturer += "* ";
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
printRightAngledTriangle(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern: 3
// 1
// 12
// 123
// 1234
// 12345

// This function will print Pattern 2 as described above
const printRightAngledTriangleWithNums = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = 1; j <= i; j++) {
      patternCapturer += `${j} `;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
printRightAngledTriangleWithNums(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern: 4
// 1
// 22
// 333
// 4444
// 55555

// This function will print Pattern 2 as described above
const printRightAngledTriangleWithIdenticalNums = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = 1; j <= i; j++) {
      patternCapturer += `${i} `;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
printRightAngledTriangleWithIdenticalNums(5);

Enter fullscreen mode Exit fullscreen mode
// Pattern : 5

// *****
// ****
// ***
// **
// *

// This function will print Pattern 5 as described above
const reversedRtAngledT = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = input; j >= i; j--) {
      patternCapturer += "* ";
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
reversedRtAngledT(5);

Enter fullscreen mode Exit fullscreen mode
// Pattern : 6

// 12345
// 1234
// 123
// 12
// 1

// This function will print Pattern 6 as described above
const reversedRtAngledTWithNums = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = input; j >= i; j--) {
      patternCapturer += `${input - (j - 1)}`;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
reversedRtAngledTWithNums(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern : 7
//    *
//   ***
//  *****

// This function will print Pattern 7 as described above
const pyramidPattern = (input) => {
  let patternCapturer = "";

  for (let i = 1; i <= input; i++) {
    // Printing Spaces
    for (let j = 1; j <= input - i; j++) {
      patternCapturer += " ";
    }

    //Printing Stars
    for (let k = 0; k < 2 * i - 1; k++) {
      patternCapturer += "*";
    }

    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};
// Providing Input value on the basis of this  Pattern Printed
pyramidPattern(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern :8
//   *****
//    ***
//     *

// This function will print Pattern 8 as described above
const reversePyramid = (input) => {
  let patternCapturer = "";

  for (let i = 0; i < input; i++) {
    // For printing spaces
    for (let j = 0; j < i; j++) {
      patternCapturer += " ";
    }

    // For printing stars
    for (let k = 0; k < 2 * (input - i) - 1; k++) {
      patternCapturer += "*";
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
reversePyramid(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern : 9
//    *
//   ***
//  *****
//  *****
//   ***
//    *

// This function will print Pattern 9 as described above
const printDiamond = (input) => {
  let patternCapturer = "";

  for (let i = 1; i <= input; i++) {
    // for spaces
    for (let j = 1; j <= input - i; j++) {
      patternCapturer += " ";
    }

    //for Stars
    for (let k = 0; k < 2 * i - 1; k++) {
      patternCapturer += "*";
    }
    patternCapturer += "\n";
  }
  for (let i = 0; i < input; i++) {
    // for spaces
    for (let j = 0; j < i; j++) {
      patternCapturer += " ";
    }
    // for stars
    for (let k = 0; k < 2 * (input - i) - 1; k++) {
      patternCapturer += "*";
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
printDiamond(5);
Enter fullscreen mode Exit fullscreen mode
// Pattern: 10
// 1
// 01
// 101
// 0101
// 10101

// This function will print Pattern 10 as described above
const printRightAngledTriangleWithBoolNums = (input) => {
  let patternCapturer = "";
  for (let i = 1; i <= input; i++) {
    for (let j = 1; j <= i; j++) {
      patternCapturer += `${(i + j) % 2 == 0 ? 1 : 0} `;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
printRightAngledTriangleWithBoolNums(5);

Enter fullscreen mode Exit fullscreen mode
// Pattern 11

// *
// * *
// * * *
// * * * *
// * * * * *
// * * * *
// * * *
// * *
// *

// This function will print Pattern 11 as described above
const rightArrowPattern = (input) => {
  let patternCapturer = "";

  for (let i = 1; i <= input; i++) {
    for (let j = 1; j <= i; j++) {
      patternCapturer += "* ";
    }
    patternCapturer += "\n";
  }

  for (let i = 1; i < input; i++) {
    for (let j = input - 1; j >= i; j--) {
      patternCapturer += "* ";
    }
    patternCapturer += "\n";
  }

  console.log(patternCapturer);
};

// Providing Input value on the basis of this  Pattern Printed
rightArrowPattern(5);

Enter fullscreen mode Exit fullscreen mode

GITHUB LINK : https://github.com/Jagroop2000/DSA-One-Javascript/blob/master/patterns/patterns.js

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player