DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

Number Patterns in Javascript

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

function printNum(){
 for(let i=1; i<=4; i++){
     let finalOutput = '';
     for(let j=1; j<=i; j++){
         finalOutput += i
     }
     console.log(finalOutput)
 }   
}

printNum()

output
1
1 2
1 2 3
1 2 3 4
Enter fullscreen mode Exit fullscreen mode
function printNum(){
 for(let i=1; i<=4; i++){
     let finalOutput = '';
     for(let j=1; j<=i; j++){
         finalOutput += j
     }
     console.log(finalOutput)
 }   
}

printNum()

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

function printNum(){
 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)
 }   
}

printNum()

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

function printNum(){
 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 += i
     }
     console.log(finalOutput)
 }   
}

printNum()

output
      1
    1 2
  1 2 3
1 2 3 4
Enter fullscreen mode Exit fullscreen mode
function printNum(){
 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)
 }   
}

printNum()

output
   1
  1 2
 1 2 3
1 2 3 4
Enter fullscreen mode Exit fullscreen mode
function printNum(){
 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 += i + " "
     }
     console.log(finalOutput)
 }   
}

printNum()

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

Top comments (0)