Reputation: 49
i want create triangle number pattern
let segitigaPola3 = num => {
let hasil = ''
for (let i = 1; i <= num; i++) {
for(let j = 0; j < i; j++) {
hasil += `${i} `
}
hasil += ` \n`
}
return hasil
}
console.log(segitigaPola3(5))
console.log()
console.log()
currently output is :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
what i want is something like for triangle :
1
2 3
4 5 4
3 2 1 2
3 4 5 4 3
and for square like :
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
how can i achieve like this? thanks for any advice :)
Upvotes: 0
Views: 587
Reputation: 1720
For the triangle, this pattern have 4 conditions:
When the number is increasing:
num
==> It's time to turn the flow to decreasingWhen the number is decreasing:
In this code below, I use now
and before
to track whether the current process/flow is increasing or decreasing, and then check if the current number reaches the turning point (1 or the num
variable).
let segitigaPola3 = num => {
let hasil = ''
let now = 1; // Set arbitrary counter
let before = 0;
for (let i = 1; i <= num; i++) {
for(let j = 0; j < i; j++) {
hasil += `${now} `;
console.log(now, before, num);
if (now > before) { // Check if the flow is increasing
if (now !== num) { // Check for the turning point (num)
now++;
before++;
} else {
now--;
before++;
}
} else {
if (now > 1) { // Check if the current still not reached the turning point
now--;
before--;
} else { // When the current reaches the turning point
now++;
before--;
}
}
}
hasil += ` \n`
}
return hasil
}
console.log(segitigaPola3(5));
For the square, there are 2 conditions for the column-based elements:
Row no. | i ( = Col no.) | j
------------------------------
1 | 1 | 1
5 | 1 | 5
------------------------------
1 | 3 | 11
5 | 3 | 15
This always starts from (i-1)*num + 1
and always increases until ends up at (i*num)
and we use j
to fill the elements.
Row no. | i ( = Col no.) | j
------------------------------
1 | 2 | 10
2 | 2 | 9
------------------------------
1 | 4 | 20
2 | 4 | 19
Based on the pattern above, this always starts from i*num
and always increases until ends up at (i-1)*num + 1
and we use j
to fill the elements.
function squareMaker(num) {
let result = '';
let arr = [];
for (let i = 1; i <= num; i++){
let temp = [];
// Generating the odd column elements
if (i % 2 != 0) {
for (let j = (i-1)*num + 1; j <= i*num; j++) {
temp.push(j);
}
}
// Generating the even column elements
else {
for (let j = i*num; j > (i-1)*num ; j--) {
temp.push(j);
}
}
arr.push(temp);
}
// Generating the result string.
for (let i = 0; i < num; i++){
for (let j = 0; j < num; j++) {
result += `${arr[j][i]} `;
}
result += '\n';
}
return result;
}
console.log(squareMaker(5));
Upvotes: 2
Reputation: 1772
For a triangle pattern u can use @depperm's function. The vertical square snake pattern is a bit trickier:
function triangle(count) {
var result = "";
var x = 1;
var add = true;
for (var i = 1; i <= count; i++) {
for(var j = 0; j < i; j++) {
result += x + " ";
add ? x += 1 : x -= 1;
if(x === count || (x === 1 && !add)) add =! add;
}
result += " \n";
}
return result;
}
function square(count) {
var result = "";
var rows = [];
for (var i = 0; i < count; i++) {
if (i == 0) {
var row = [0];
for (var j = 0; j < count; j++) {
var num = (j + 1) % 2 == 0 ? (j + 1) * count : row[j] + 1;
row.push(num);
}
row.shift();
} else {
var row = [];
for (var j = 0; j < count; j++) {
var num = (j + 1) % 2 == 0 ? (rows[i - 1][j] - 1) : (rows[i - 1][j] + 1);
row.push(num);
}
}
rows.push(row);
result += row.join(" ") + "\n";
}
return result;
}
console.log(triangle(5));
console.log(square(5));
Upvotes: 2
Reputation: 10746
For the triangle, I'd create a separate variable for the output number (x
) and a flag for if it should add or subtract.
let segitigaPola3 = num => {
let hasil = ''
let x=1;
let add=true;
for (let i = 1; i <= num; i++) {
for(let j = 0; j < i; j++) {
hasil += `${x} `
add?x+=1:x-=1;
if(x===num || (x===1 && !add)) add=!add
}
hasil += ` \n`
}
return hasil
}
console.log(segitigaPola3(5))
Upvotes: 1