0% found this document useful (0 votes)
10 views

Parttern Program

The document contains multiple code snippets for creating patterns and numbers using JavaScript loops and conditionals. It also includes examples for checking Armstrong numbers, calculating aspect ratios, and sorting arrays.

Uploaded by

fgondaliya721
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Parttern Program

The document contains multiple code snippets for creating patterns and numbers using JavaScript loops and conditionals. It also includes examples for checking Armstrong numbers, calculating aspect ratios, and sorting arrays.

Uploaded by

fgondaliya721
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Create this parrtrn using js

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9

let n = 5;
let s = "";
for (let i = 1; i <= n; i++) {

for (let j = 1; j <= i; j++) {


s += "";
}

let num = i;
for (let k = 1; k <= i; k++) {
s += num + " ";
num++;
}

s += "\n";
}

console.log(s);

2) create this parttrn using js

1
4 9
64 125 216

let a = 3;
let s = "";
count = 1;

for (let i = 0; i <= a; i++) {


for (let j = 1; j <= i; j++) {
if( i == 3){
s += count * count * count + " ";
} else{
s += Math.pow(count, 2) + " ";
}
count++;
}
s += "\n";
}

console.log(s);

create parrten program


12
9 6
3 0 3
6 9 12 15

let decrement = 12;


let increment = 3;

for (let i = 0; i < 3; i++) {


let row = '';
for (let j = 0; j <= i; j++) {
if (i % 2 === 0) {
row += decrement + ' ';
decrement -= 3;
} else {
row += increment + ' ';
increment += 3;
}
}
console.log(row);
}

let a = 4;
let str = "";
counter = 12;
let b = 0;

for (let i = 1; i <= 4; i++) {


for (let j = 1; j <= i; j++) {
if( a >= 1 ){
str += a * 3 + " ";
a--;
} else{
str += b * 3 + " ";
b++;
}

}
str += "\n";
}

console.log(str);

create fibonaci series

1,1,2,3,5,8,13,21

let n = 0,
m = 1,
num;
l = prompt("enter number")
for (let i = 1; i <= l; i++) {
console.log(n);

num = n + m;
n = m;
m = num;
}

create this pattern program ?

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

let n = 5;

for(let i = 1; i <= n; i++){

let p = " ";

for(let j = 1; j <= i; j++){

if(i + j) % 2 === 0){

p += "1 ";

}else{

p += "0 ";

console.log(p);

create * pattern program ?

******
******
******
******
******
******

Code:

let n = 6;
let s = "";

for (let i = 0; i < n; i++) {


for (let j = 0; j < n - i - 1; j++) {
s += " ";
}

for (let k = 0; k < n; k++) {


s += "*";
}

s += "\n";

console.log(s);

Create number pattern program.

000000
0 0
0 0
0 0
0 0
000000

Code :

let n = 6;
let s = "";

for (let i = 0; i < n; i++) {


for (let j = 0; j < n; j++) {
if (i === 0 || i === n - 1 || j === 0 || j === n - 1) {
s += "0 ";
} else {
s += " ";
}
}
s += "\n";
}

console.log(s);

* *
** **
*** ***
**** ****
***** *****
************
************
***** *****
**** ****
*** ***
** **
* *

Code:

let n = 5;

for (let i = 1; i <= n; i++) {


let s = "";
for(let j = 1; j <= i; j++){
s += "*";
}
for(let k = n; k > i; k--){
s += " ";
}
for(let l = 1; l <= i; l++){
s += "*";
}
console.log(s);
}

for (let i = n; i >= 1; i--) {


let s = "";
for (let j = 1; j <= i; j++) {
s += "*";
}
for (let k = n; k > i; k--) {
s += " ";
}
for (let l = 1; l <= i; l++) {
s += "*";
}
console.log(s);
}

Create number pattren ?

6
65
654
65432
65432
654321
6543210
654321
65432
6543
654
65
6

Code :

let n = 6;
let s = "";

for (let i = n; i >= 0; i--) {


for (let j = n; j > i; j--) {
s += j;

}
s += "\n";
}

for (let i = 0; i <= n; i++) {


for (let j = n; j >= i; j--) {
s += j;
}
s += "\n";
}

console.log(s);

Create pattern Program

1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

code :

let n = 5;
let x;
let y;
let s = "";
for (let i = 1; i <= n; i++) {
x = i;
y = n - i + 1;

for (let j = 1; j <= n; j++) {

if (j % 2 == 1) {
s += `${`%2d`, x} `;
} else {
s += `${`%2d`, y} `;
}
x += n;
y = y + n;
}
s += "\n";
}
console.log(s);

create armstorge number and check armstorg or not ?

// let number = prompt("Enter a number: ");


// let sum = 0;
// let s = number;

// while (s > 0) {
// let digit = s % 10;
// sum += digit ** 3;
// s = parseInt(s / 10);
// }

// if (sum === number) {


// console.log(number + " is an Armstrong number");
// } else {
// console.log(number + " is not an Armstrong number");
// }

find a aspect ratio of two numbers ?

let num1 = prompt("Enter first number:");


let num2 = prompt("Enter second number:");

let ratio = num1 / num2;

console.log("The aspect ratio of " + num1 + " and " + num2 + " is " + ratio);

armstrog number check ?

let n = 153;

let num = n;
while (num > 0) {
let r = num % 10;
sum += r ** 3;
num = parseInt(num / 10);
}

if (sum === n) {
console.log(`${n} is Armstrong number`);
} else {
console.log(`${n} is not Armstrong number`);
}

arry sort::::::::::::::::::::::

let data = [
[20, 1, 2014],
[25, 3, 2010],
[3, 12, 1676],
[18, 11, 1982],
[19, 4, 2015],
[9, 7, 2015]
];
for (let i = 0; i < data.length - 1; i++) {
for (let j = 0; j < data.length - i - 1; j++) {
if (data[j][2] > data[j + 1][2]) {
let temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
console.log(data);

let arr = [
{ date: '2014-01-14' },
{ date: '2010-03-25' },
{ date: '1976-12-03' },
{ date: '1982-11-18' },
{ date: '2015-04-19' },
{ date: '2015-07-09' }
];

for (let i = 0; i < arr.length; i++) {


for (let j = 0; j < arr.length - i - 1; j++) {
if (new Date(arr[j].date) > new Date(arr[j + 1].date)) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

console.log(arr);

You might also like