How to Make a Triangle Using for Loop JavaScript

How to make a triangle using for loop javascript

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("   ");
}
for (var j = 5; j > i; j--) {
document.write("#");
if (j > i + 1) document.write(" ");
}
document.write('<br/>')
}

https://js.do/code/diamondsinthesky

how do i make a triangle using for loop in javascript which output matches the parameter content

let printTriangle = (num) => {
let result = "";
for ( let i = 0; i < num; i++) {
for (let j = 0; j <= i; j++) {
result += '*';
}
result += "\n";
}
return result;
}
console.log(printTriangle(5));

how to make isosceles triangle in javascript?

The below code will give you the desired output.

    function pyramid(N){
let result = "";
for(let i = 1 ; i <= N ; i++) {
for(j = N ; j > i ; j--) {
result += " ";
}
for(j = 1 ; j<= i ; j++) {
result += i + " ";
}
result += "\n";
}
return result;
}
console.log(pyramid(5));

looping a triangle with a while loop

It's the result of the final expression in your code. Type in "5+5" in your JS console and you'll get a result. If you put "5+5" at the end of a script, same thing. That's what's happening here:

var result = "#"
while(result.length <=7 ){
console.log(result);
result = result + "#"; // <-- this is the last statement executed, so it is returned
}

In contrast, the last statement below is a logging statement, which has no return value. So the result of the script is undefined.

for (var result = "#"; result.length <=7; result = result + "#")
console.log(result);

You can see this more clearly if you try something like this:

for (var i=0; i < 5; i++) { 
console.log(i);
i
}

Here, the last expression is simply i. This code prints each number from 0-4 inclusive, then prints the 4 a second time because its the final expression.

0
1
2
3
4 <-- the final console.log() call
4 <-- the final expression

how to make a row of triangles using loops?

You can use the iteration (i) and multiply it by the spacing you want and add it to the x value.

function showDrawing() {
let coolCanvas = document.getElementById("canvas");
let ctx = coolCanvas.getContext("2d");
ctx.lineWidth = 5;

for (let i = 0; i < 5; i += 1) {
ctx.beginPath();
ctx.moveTo(126+(i*170), 300);
ctx.lineTo(200+(i*170), 400);
ctx.lineTo(50+(i*170), 400);
ctx.closePath();
ctx.strokeStyle = 'blue';
ctx.fillStyle = 'purple';
ctx.fill();
ctx.stroke();
}
}
<canvas id="canvas" width="1500" height="700" style="border:3px solid #000000;">
</canvas>

<button onclick="showDrawing()">Drawing</button>

Upside down triangle using one for-loop

Your code is fine, Just get rid of triangleStr and replace it inside for-loop with method repeat.

function triangle(num) {
for (let i = num; i > 0; i--)
{
console.log("#".repeat(i));
}
}
triangle(5);

Result

#####
####
###
##
#

How to use a For Loop to make a triangle out output of the inputted numbers in JavaScript

Look at the documentation for window.prompt().

Remove .value. input is the value.

Also, you aren't telling your code to not run if input is "bad".

// Be in a function so that you can return(function() {
var input = prompt("Enter a number: ");
if (!input) { alert("Either you entered a NaN or you left an empty field. \nPlease enter some number!"); // Get of of the function return; }
for (input = 10; input <= 15; input++) { var a = ''; for (var j = 10; j <= input; j++) { var a = a + '' + input; }
document.writeln(a + "<BR>");
}}());

Shorter Way of Prinitng Triangle Patterns JavaScript

If you want to use only one loop, you can use repeat() method in ECMAScript6 or higher.

For Right triangle: