-1

I have a simple question although i cannot manage to resolve this problem. Hope you can help. I need to make triangle using for loop and from this 4 exercises I don't know what to do with the third one. I haven't used Javascript before, so any help would be appreciated.

enter image description here

 # # # # #
   # # # #
     # # #     <----- here is triangle i need to make. Just in case 
       # #
         #

var i;
var j;
for (i = 0; i <= 5; i++ )
{
document.write("</br>");
for ( j = 0; j < 6-i; j++ )
{
document.write( "&nbsp&nbsp" );
}
for ( j = 6-i; j <= 5; j++ )
{

document.write( "*" );
}
}

This is code I wrote for D in photo. And I'm sorry i did not add it at first.

2
  • 1
    You need to add what you've tried so far Commented Oct 27, 2016 at 20:10
  • Check out the how to ask... Commented Oct 27, 2016 at 20:12

8 Answers 8

4
for (let line = "*"; line.length < 8; line += "*")
  console.log(line);

this question came in this book: http://eloquentjavascript.net

I don't know why there are so bad answers on google for this one.

Sign up to request clarification or add additional context in comments.

Comments

2
function leftTriangle(rows){
  let result = '';
  for(let i=rows;i>0;i--){
    if(i===rows) {
      result += '*'.repeat(i) + '\n';
    }else{
      let empty = rows-i
      result+= ' '.repeat(empty) + '*'.repeat(i)+ '\n'
    }
  }
  return result;
}

console.log(leftTriangle(5))

Comments

0

I'm sure there are better solutions (simply left-padding with spaces comes to mind), but here's the quick and dirty one I created from your own solution.

  for (var i = 0; i < 5; i++) {
    for (var j = 0; j < i; j++) {
      document.write("&nbsp;&nbsp;&nbsp;");
    }
    for (var j = 5; j > i; j--) {
      document.write("#");
      if (j > i + 1) document.write("&nbsp;");
    }
    document.write('<br/>')
  }

https://js.do/code/diamondsinthesky

1 Comment

YES))) I think your solution is much better then mine. Thank you very very much.
0

Something like this?

var rows = 5;
for (var i = rows; i--;) {
  var columns = 0;
  while (columns <= i) {
    document.write('#');
    columns++
  }
  document.write('<br />\n');
}

1 Comment

Thank you for your response. Unfortunately it didn't help because it gives triangle which i already made. Triangle B from the photo above.
0

Thank you for your help. I did it. It was too obvious but somehow I couldn't find it. Thank you one more time. Here is how i did it.

 for (i = 5; i > 0; i--) {
         document.write("</br>");
        for (j = 0; j < 6 - i; j++) {
            document.write("&nbsp&nbsp");
        }
        for (j = 6 - i; j <= 5; j++) {

            document.write("*");
        }
    }

Comments

0

var rows = 5;
for (var i = rows; i--;) {
  var columns = 0;
  while (columns <= i) {
    document.write('#');
    columns++
  }
  document.write('<br />\n');
}

1 Comment

That generates triangle (A) from the question, but not triangle (C), which is specifically the one the OP is asking for help
0

You can also do this if you are looking for something different. This code is for a triangle of 7 lines.

let size = 8;
let y = "#";
for (let x = 0; x < size; x++)
{
  console.log(y);
  y += "#";
}

// Second method

for (let i = 1; i < size;i++)
{
  let me ="#".repeat(`${i}`)
  console.log(me);
  
}

2 Comments

It appears you didn't address the question... poster asked how to produce the third result, not the first or second.
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
0

  var size = 5;
    for (var i = 0; i < size; i++) {
       for (var j = 0; j <= i; j++) {
            document.write("*");
          }
          document.write("<br />\n");
        }

1 Comment

Please read How do I write a good answer?. While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.