最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to print a half pyramid in javascript - Stack Overflow

programmeradmin0浏览0评论

The code works but I don't want the inner for loop to take me to the new line.

for (i = 0; i < 5; i++) {
  for (j = 1; j <= i; j++) {
    console.log('*');
  }
  console.log();
}
console.log('-----------------');

The code works but I don't want the inner for loop to take me to the new line.

for (i = 0; i < 5; i++) {
  for (j = 1; j <= i; j++) {
    console.log('*');
  }
  console.log();
}
console.log('-----------------');

Share Improve this question edited Apr 9, 2018 at 9:55 Lorenzo Sciuto 1,6853 gold badges28 silver badges57 bronze badges asked Apr 9, 2018 at 9:43 Aiman KaziAiman Kazi 211 silver badge5 bronze badges 4
  • 1 Instead of logging directly, construct a string inside the inner loop and print it after the loop pletes. – Federico klez Culloca Commented Apr 9, 2018 at 9:44
  • What is your expected output? – Ankit Agarwal Commented Apr 9, 2018 at 9:44
  • This code works? – Jeremy Thille Commented Apr 9, 2018 at 9:44
  • Guys, the question seems rather clear. At the moment the code is printing a newline after each '*' and OP doesn't want that. – Federico klez Culloca Commented Apr 9, 2018 at 9:45
Add a ment  | 

9 Answers 9

Reset to default 1

console.log will automatically break the line. Concatenate to a string instead of a log. Log at the end.

let str = '';

for(i = 0; i <= 5 ; i++) {
    for(j = 1; j <= i; j++) {
       str += '*';
    }
    str += '\n';
}
console.log(str);

You can do this way, with the help of a string variable:

for (i = 0; i < 5; i++) {
  var str = '';
  for (j = 1; j <= i; j++) {
    str+='*';
  }
  console.log(str);
}
console.log('-----------------');

If you want to print at the page, use like below

   for (i = 0; i < 5; i++) {
      let j=0;
      do{document.write("*");j++;}while(j < i)
      document.write("<br/>")
    }
let string = "";

for (let i = 0; i < 5; i++){
    string += '*';
    console.log(string);
}

Output:

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

You need to break the line with the console.log you can also controle the space between * with output+='*' + " ";

function pyramid() {
    var total = 5;
    var output="";
    for (var i = 1; i <= total; i++) {
        for (var j = 1; j <= i; j++) {
            output+='*' + " ";
        }
        console.log(output);
        output="";
    }
}  

pyramid()

You can get rid of second for loop as follows:

 var str = '';
for (i = 1; i <= 5; i++) {
    str +=Array(i).join("*");
    str +='\n';
}
console.log(str);

A simple way to solve this "exercise" in JavaScript:

let width = ""
while(width.length < 6) console.log(width += `#` );

Basically, we create a string (width) and increment its value using the while loop till we hit a restriction.

I found the more typical method "bulky"(?)...plus there's the issue of not getting the exact picture of a half pyramid.

let i,j 

for (i= 0; i < 6; i++){

    for (j = 0; j<=i; j++){
        console.log("#")
    }
    console.log("\n")
}
function pyramid(n){   
    let result = "";    
    for(let i=0; i<=n; i++){  
        result += "*".repeat(i);  
        result += "\n"  
    }   
    return result;   
}
console.log(pyramid(5));
//OutPut
*
**
***
****
*****

As we need n number of pyramid structure with '' / '#' / any symbol. by using above code we can achieve. Here you can see we just created a function called pyramid with one parameter 'n'. and inside function we declare a variable 'result'. So inside for loop the length of 'i' is "<=n" and also you can use "repeat() method to print '' 'i' times. So if you call that function like console.log(pyramid(5)); You can able to see your Answer as expected..

shortest code:

console.log('*\n**\n***\n****\n*****');

发布评论

评论列表(0)

  1. 暂无评论