我想創建三角形數字圖案
let segitigaPola3 = num => {
let hasil = ''/span>
for (let i = 1; i <= num; i ) {
for(let j = 0; j < i; j ) {
hasil = `${i} `。
}
hasil = ` }
`
}
return hasil
}
console.log(segitigaPola3(5))。
console.log()
console.log()/code
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
目前的輸出是:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
我想要的是這樣的東西 對于三角形 :
1
2 3
4 5 4
3 2 1 2
3 4 5 4 3
而對于正方形,如 :
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
我怎樣才能實作這樣的目標? 感謝任何建議:)
uj5u.com熱心網友回復:
對于一個三角形圖案,你可以使用@depperm的函式。
。function triangle(count) {
var result = ""/span>;
var x = 1;
var add = true;
for (var i = 1; i <= count; i ) {
for(var j = 0; j < i; j ) {
result = x " "/span>;
add ? x = 1 : x -= 1;
if(x == count || (x == 1 && !add)) add =! add;
}
result = "
"。
}
return result。
}
function square(count) {
var result = ""/span>;
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(" "/span>) "
"。
}
return result。
}
console.log(triangle(5) )。
console.log(square(5));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
對于三角形,這個模式有4個條件:
如果你是一個人,那么你就必須要有一個人的名字。
當數字在增加時:
。
- 如果當前的數字與
num==>相同,就應該將流量轉為遞減 。
- 否則,保持原樣。
當數字是遞減的時候:
在下面這段代碼中,我使用now和before來跟蹤當前行程/流程是增加還是減少,然后檢查當前數字是否達到轉折點(1或num變數)。
let segitigaPola3 = num => {
let hasil = ''/span>
let now = 1; //設定任意計數器。
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 { // 當電流到達轉折點時
now 。
before--。
}
}
}
hasil = `
`
}
return hasil
}
console.log(segitigaPola3(5) ;
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
對于廣場,基于列的元素有2個條件:
對于廣場,基于列的元素有2個條件。
Row no. | i ( = Col no.) | j
------------------------------
1 | 1 | 1 1
5 | 1 | 5 !
------------------------------
1 | 3 | 11
5 | 3 | 15 !
這總是從(i-1)*num 1開始,并且總是增加,直到最后達到(i*num),我們用j來填充這些元素。
- 對于偶數列,情況正好相反。突出顯示以查看模式:
Row no. | i ( = Col no.) | j
------------------------------
1 | 2 | 10 !
2 | 2 | 9
------------------------------
1 | 4 | 20 !
2 | 4 | 19
基于上面的模式,這總是從i*num開始,并且總是增加,直到最后在(i-1)*num 1結束,我們使用j來填充元素。
function squareMaker(num) {
let result = ''/span>;
let arr = [];
for (let i = 1; i <= num; i ){
let temp = [];
// 生成奇數列元素。
if (i % 2 != 0) {
for (let j = (i-1)*num 1; j <= i*num; j ) {
temp.push(j)。
}
}
// 生成偶數列元素。
else {
for (let j = i*num; j > (i-1)*num; j--) {
temp.push(j)。
}
}
arr.push(temp)。
}
//生成結果字串。
for (let i = 0; i < num; i ){
for (let j = 0; j < num; j ) {
result = `${arr[j][i]} `。
}
result = '
'。
}
return result。
}
console.log(squareMaker(5));
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
對于三角形,我會為輸出數字(x)創建一個單獨的變數,并為它是否應該添加或減去一個標志。
let segitigaPola3 = num => {
let hasil = ''/span>
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=!
}
hasil = `.
`
}
return hasil
}
console.log(segitigaPola3(5)) /code>
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/312050.html
標籤:
上一篇:錯誤。對JDK1.8或更高版本的要求檢查失敗。我正在使用java16.0。
下一篇:從串列中提取
