How to make reverse asterisk Triangle with JavaScript using 'for' loop - javascript

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!

Add 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('&nbsp&nbsp');
}
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 come first. You can loop in ascending fashion with value of i denoting spaces and n-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);

Related

I'm trying to create a star pyramid which should contain 1 * in the first row, 2* with equal spaces in the second row, like wise 3rd and 4th row

[enter image description here][1]
List item
let x = 4;
let text = "";
for(let i = 1; i <= x; i++) {
for(let j = 1; j <= x - i; j++) {
text += " ";
}
for(let k = 0; k < 2 * i - 1; k++) {
text += "*";
}
text +="\n";
}
console.log(text);
---Output I want is like this. I'm new to this. Thank you in advance
[1]: https://i.stack.imgur.com/AJvaZ.png
Small change:
let x = 4;
let text = "";
for(let i = 1; i <= x; i++) {
for(let j = 1; j <= x - i; j++) {
text += " ";
}
text += "*";
for(let k = 0; k + 1 < i; k++) {
text += " *";
}
text +="\n";
}
console.log(text);

maybe you just know what makes the asterisks on this loop so much

var s = '';
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 10; j++) {
s += '*'
}
s += '\n';
console.log(s)
}
maybe you just know what makes the asterisks on this loop so much?
even though I only want to repeat it for 5 lines, but why are there so many results?
[1]: https://i.stack.imgur.com/7zzOu.png
Your code is producing the following result:
Because not resetting the var s inside the outter iteration.
Instead, you may do:
for (let i = 0; i < 5; i++) {
let s = '';
for (let j = 0; j < 10; j++) {
s += '*';
}
s += '\n';
console.log(s);
}
which will produce the expected result.

Print pyramid number using JavaScript

I want to create a pyramid number structure. Can anyone explain me how?
for (i = 1; i <= 5; i++) {
var k = ' ';
var myspace = '';
for (j = 0; j < i - 0; j++) {
k += i;
myspace += ' ';
}
console.log(myspace + k);
}
I want make it like this:
1
2 2
3 3 3
4 4 4 4
Try below code:
function createPyramid(rows) {
for (var i = 0; i < rows; i++) {
var output = '';
for (var j = 0; j < rows - i; j++) output += ' ';
for (var k = 0; k <= i; k++) output += (i + 1) + ' ';
console.log(output);
}
}
createPyramid(4);
Try below method.
var a;
var n = 5;
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= i; j++) {
document.write(j + " ");
}
document.write("<br />");
}

How to make a triangle using for loop javascript

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.
# # # # #
# # # #
# # # <----- 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.
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.
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))
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
Something like this?
var rows = 5;
for (var i = rows; i--;) {
var columns = 0;
while (columns <= i) {
document.write('#');
columns++
}
document.write('<br />\n');
}
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("*");
}
}
var rows = 5;
for (var i = rows; i--;) {
var columns = 0;
while (columns <= i) {
document.write('#');
columns++
}
document.write('<br />\n');
}
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);
}
var size = 5;
for (var i = 0; i < size; i++) {
for (var j = 0; j <= i; j++) {
document.write("*");
}
document.write("<br />\n");
}

display pyramid in javascript in the below format

I am trying to display the pyramid in the console part using only javascript except document.write("")
*
* *
* * *
* * * *
* * * * *
I don't want in the above format.
I want in the below format
*
* *
* * *
* * * *
* * * * *
My code is
function getPyramid(param) {
for (var i = 1; i <= param; i++) {
var output = "";
for (var j = 1; j <= i; j++) {
output += j + " ";
}
console.log(output);
output = "";
}
}
<button onclick="getPyramid('10')">
function renderPyramid(n) {
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 + '*';
}
console.log(str);
}
}
renderPyramid(5)
my solution
function pyramid(n) {
// generate base of pyramid, aka longest possible string
let limit = n+n-1;
let hashesToPrint = 1; // number of hashes to print
for (let i=0; i<n; i++) {
// get length of spaces we need on each side
let difference = (limit - hashesToPrint) / 2;
// generate spaces string
let spaces = ' '.repeat(difference);
// create pounds string
let pounds = '#'.repeat(hashesToPrint);
// append spaces on either side of our pound string
let newString = spaces + pounds + spaces
console.log(newString)
// increment our counter by two
hashesToPrint += 2
}
}
pyramid(3)
Resolved :I got logic for this question
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<button onclick="getPyramid('10')">
Get Pyramid</button>
<script>
function getPyramid(param)
{
for(var i=0;i<param;i++) {
var output="";
for(var j=0;j<param-i;j++) {
output+=" ";
// console.log(" ");
}
for(var k=0;k<=i;k++) {
// console.log("* ");
output += "* ";
}
// output += "";
console.log(output);
}
}
</script>
</body>
</html>
<script type="text/javascript">
var i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
document.write("\t*");
}
document.write("<br>");
}
for (i = 5; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
document.write("\t*");
}
//document.write("\t");
document.write("<br>");
}
for (i = 5; i >= 0; i--)
{
for (j = 0; j < i; j++)
{
document.write("*");
}
for (k = i; k <= 5; k++)
{
document.write("*");
}
document.write("*");
}
</script>

Categories