I want to make a reverse triangle using 'for' loop, which should look like this:
*****
****
***
**
*
This what I got:
*****
****
***
**
*
And this is my code:
function rightTriangle(n) {
for (var i = 0; i <= n; i++) {
for (var j = n - 1; j >= i; j--) {
document.write('*');
}
document.write('<br>');
}
}
rightTriangle(5);
I want to make a reverse triangle using 'for' loop, which should look like this:
*****
****
***
**
*
This what I got:
*****
****
***
**
*
And this is my code:
function rightTriangle(n) {
for (var i = 0; i <= n; i++) {
for (var j = n - 1; j >= i; j--) {
document.write('*');
}
document.write('<br>');
}
}
rightTriangle(5);
Please help me out with this task, I would be so appreciated!
Share Improve this question edited Mar 24, 2017 at 9:56 Rajesh 25k5 gold badges50 silver badges83 bronze badges asked Mar 24, 2017 at 9:55 Nomad Jensen MontrealNomad Jensen Montreal 1111 gold badge3 silver badges10 bronze badges5 Answers
Reset to default 4Add the below code to leave the intendation
for(var k=0; k<i; k++){
document.write(" ");
}
function rightTriangle (n) {
for (var i = 0; i <= n; i++) {
for(var k=0; k<i; k++){
document.write(" ");
}
for (var j = n-1; j >= i; j--) {
document.write('*');
}
document.write('<br>');
}
}
rightTriangle(5);
html{
font-family:monospace;
}
function rightTriangle(n) {
for (var i = 0; i <= n; i++) {
for (var j = 0; j <= n; j++) {
if(j>=i) document.write('*');
else document.write('  ');
}
document.write('<br>');
}
}
rightTriangle(5);
Algo
- Create n*n matrix with 2 values,
" "
and"*"
- In html
" "
has no effect as extra spaces are trimmed. You will have to use
, unicode value for single space. - Now loop and add necessary parts to string.
- Since you need reverse triangle, spaces will e first. You can loop in ascending fashion with value of
i
denoting spaces andn-i
denoting "*"
function rightTriangle(n) {
var html = "";
for (var i = 0; i < n; i++) {
for(var j = 0; j< i; j++){
html += " ";
}
for(var k = 0; k< n-i; k++){
html += "*"
}
html +="<br/>"
}
document.querySelector('.content').innerHTML = html
}
rightTriangle(5);
.content {
font-family: monospace;
}
<div class="content"></div>
string.repeat
function rightTriangle(n) {
var html = "";
for (var i = 0; i < n; i++) {
html += " ".repeat(i) + "*".repeat(n - i) + "</br>"
}
document.querySelector('.content').innerHTML = html
}
rightTriangle(5);
.content {
font-family: monospace;
}
<div class="content"></div>
Also note that using document.write and < br/>" are considered bad practice.
Here is the most simple solution:
const prompt = require("prompt-sync")();
let n = parseInt(prompt("Enter the number of rows: "));
let string = "";
function printPyramid(row) {
for (let i = 0; i < n; i++) {
string += " ".repeat(i) + "*".repeat(n - i);
string += "\n";
}
}
printPyramid(n);
process.stdout.write(string);
- This code must work on ur terminal or any online js piler.
// 3 for loops are used here; i for rows, j for spaces , k for printing stars
function invertedRightTri(n){
var str="";
for(i=1;i<=n;i++){
for(j=n-i;j<n;j++){
str+=" ";
}
for (k=0;k<n-i+1;k++){
str+="*";
}
str+="\n";
}
return str;
}
var ans=invertedRightTri(5);
console.log(ans);