DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

Reverse Number Patterns in Javascript

Here are some of the number Patterns in javascript that are mostly asked in Interview Questions.


function printPattern() {
  // First half of the pattern
  for (let i = 1; i <= 4; i++) {
    let finalOutput = '';
    for (let j = 1; j <= i; j++) {
      finalOutput += j;
    }
    console.log(finalOutput);
  }

  // Second half of the pattern
  for (let i = 4; i >= 1; i--) {
    let finalOutput = '';
    for (let j = 1; j <= i; j++) {
      finalOutput += j;
    }
    console.log(finalOutput);
  }
}

printPattern();


output
1
12
123
1234
1234
123
12
1

Enter fullscreen mode Exit fullscreen mode
function printPattern() {
  // First half of the pattern
  for (let i = 1; i <= 4; i++) {
    let finalOutput = '';
    for(let k=1;k<=4-i;k++){
        finalOutput += " "
    }
    for (let j = 1; j <= i; j++) {
      finalOutput += j + " ";
    }
    console.log(finalOutput);
  }

  // Second half of the pattern
  for (let i = 4; i >= 1; i--) {
    let finalOutput = '';
      for(let k=1;k<=4-i;k++){
        finalOutput += " "
    }
    for (let j = 1; j <= i; j++) {
      finalOutput += j + " ";
    }
    console.log(finalOutput);
  }
}

printPattern();

output
   1 
  1 2 
 1 2 3 
1 2 3 4 
1 2 3 4 
 1 2 3 
  1 2 
   1 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)