必须做模式问题:第2部分[JavaScript]
#javascript #编程 #patterns #dsa

学习了如何在JavaScript中构建一些基本模式后。我带有 part-2 具有更复杂的模式。

如果您直接进入第二个博客,请查看我的第一个博客: Must do Pattern Questions : Part 1 [Javascript]

因此,今天在这一部分中,我分享 5 模式,我们必须考虑到博客第1部分中使用的以前的模式多一点,我们将在JavaScript中解决此问题。<<<<<<<<<<<<<<< br>

// 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);
// 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);
// 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);
// 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);

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

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);

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