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
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
Top comments (0)