DEV Community

Cover image for Must do pattern questions :Part-2 [ Javascript]
Jagroop Singh
Jagroop Singh

Posted on

Must do pattern questions :Part-2 [ Javascript]

After learning how to build some basic pattern in Javascript. I come with with part-2 with more complex patterns.

If you directly come to this second blog , then please checkout my first blog of : Must do Pattern Questions : Part 1 [Javascript]

So today In this part I am sharing 5 patterns where we have to think a bit more than previous patterns that is available in Part-1 of the blog and we will solve this in JavaScript.

// 1
// 2 3
// 4 5 6
// 7 8 9 10
// 11 12 13 14 15

const pattern = (n) => {
  let patternCapturer = "";
  let counter = 0;
  for (let i = 0; i < n; i++) {
    for (let j = 0; j <= i; j++) {
      counter++;
      patternCapturer += `${counter} `;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};

pattern(5);
Enter fullscreen mode Exit fullscreen mode
// A
// AB
// ABC
// ABCD
// ABCDE

const alphabetPattern = (n) => {
  let patternCapturer = "";
  for (let i = 1; i <= n; i++) {
    let alphabetIndex = 65;
    for (let j = 1; j <= i; j++) {
      patternCapturer += `${String.fromCharCode(alphabetIndex)}`;
      alphabetIndex++;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};
alphabetPattern(5);
Enter fullscreen mode Exit fullscreen mode
// ABCDE
// ABCD
// ABC
// AB
// A

const alphabetPatternReverse = (n) => {
  let patternCapturer = "";
  for (let i = 1; i <= n; i++) {
    let alphabetIndex = 65;
    for (let j = n; j >= i; j--) {
      patternCapturer += `${String.fromCharCode(alphabetIndex)}`;
      alphabetIndex++;
    }
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};
alphabetPatternReverse(5);
Enter fullscreen mode Exit fullscreen mode
// A
// BB
// CCC
// DDDD
// EEEEE

const alphabetPatternIdentical = (n) => {
  let patternCapturer = "";
  let alphabetIndex = 65;
  for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= i; j++) {
      patternCapturer += `${String.fromCharCode(alphabetIndex)}`;
    }
    alphabetIndex++;
    patternCapturer += "\n";
  }
  console.log(patternCapturer);
};
alphabetPatternIdentical(5);

Enter fullscreen mode Exit fullscreen mode
// *****
// *   *
// *   *
// *   *
// *****

const hollowSquarePattern = (n) => {
  let patternCapturer = "";
  for (let i = 1; i <= n; i++) {
    if (i == 1 || i == n) {
      for (let j = 1; j <= n; j++) {
        patternCapturer += "* ";
      }
    } else {
      for (let j = 1; j <= n; j++) {
        if (j == 1 || j == n) {
          patternCapturer += "* ";
        } else {
          patternCapturer += " ";
        }
      }
    }
    patternCapturer += "\n";
  }

  console.log(patternCapturer);
};

hollowSquarePattern(5);

Enter fullscreen mode Exit fullscreen mode

Github Link :
https://github.com/Jagroop2000/DSA-One-Javascript/blob/master/patterns/patterns-part2.js

Top comments (4)

Collapse
 
works profile image
Web

I am waiting for the part 2 of this and it's finally there.

Collapse
 
jagroop2001 profile image
Jagroop Singh

I'm happy that my content is useful for you !!

Collapse
 
sergeyleschev profile image
Sergey Leschev

You have done a great job in exploring some complex patterns using Javascript.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks you @sergeyleschev !!