0

How can i make pyramid by use javaScript

I want to do like this 1 232 34543 4567654 This is my code can somebody help me i try for 5 days to solve it. Thank you for help me var n = 8; for (var i = 0; i < n; i++) { var str = ''; for (var j = 1; j < n-i; j++) { str = str + ''; } for (var k = 1; k <= (2*i+1); k++) { str = str + '*'; } //for (var l = 1; l < n; l--) { // str = str + 'k'; // } console.log(str); }

1st Feb 2021, 1:55 PM
Sippakorn Thanasarn
Sippakorn Thanasarn - avatar
6 Answers
+ 2
// only for loops: function shape(n) { var i, j, k, s = ''; for (i=0; i<n; ++i) { k = i+1, s = s && s+'\n'; for (j=0; j<n-k; ++j) s += ' '; for (j=0; j<k; ++j) s += (k+j)%10; for (j=i-1; 0<=j; --j) s += (k+j)%10; } return s; } document.write('<pre>'+shape(8)+'</pre>'); // pattern reflect pattern in your question, not pattern in video (increase vs decrease digits)
1st Feb 2021, 5:11 PM
visph
visph - avatar
+ 1
Are u trying to print * or numbers ?
1st Feb 2021, 2:04 PM
Abhay
Abhay - avatar
+ 1
did you try to get: var dig = (v,i) => (v+i)%10, rev = s => [...s].reverse().join(''), row = (v,i) => ' '.repeat(n- ++i)+(v = Array(i).fill(i).map(dig).join(''))+rev(v.slice(0,-1)), shape = n => Array(n).fill().map(row).join('\n'); document.write('<pre>'+shape(8)+'</pre>');
1st Feb 2021, 4:15 PM
visph
visph - avatar
+ 1
so, just adapt my code by replacing the 'row' map function with outer for loop, the 'dig' map function with inner for loop, and if needed also for what you want ^^
1st Feb 2021, 4:19 PM
visph
visph - avatar
0
i want to use for loop to make it like this video https://youtu.be/VKJNLp5ZKb4 but i cannot do it in javascript
1st Feb 2021, 4:16 PM
Sippakorn Thanasarn
Sippakorn Thanasarn - avatar
0
thank you very much sir
1st Feb 2021, 5:15 PM
Sippakorn Thanasarn
Sippakorn Thanasarn - avatar