Print pyramid number using JavaScript - 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 />");
}

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);

JavaScript pyramid

I'm trying to print a pyramid that has odd numbers for edges. For example output should be:
.......5
...3,4,3
1,2,3,2,1
(dots are here just to show formating)
I managed to write:
function p(n){
let string = "";
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n - i; j++) {
string += " " ;
}
for (let k = 1; k <= 2*i-1; k++) {
string += n-k +1;
}
string += "\n";
}
console.log(string);
}
p(5);
But I'm not sure how to continue to get wanted result
You can try it :
function p(n){
let string = ""
for (let i = 0; i <= n/2; i++) {
let k = n - 2 * i
for (let j =0; j < Math.floor(n/2)*2 + 1; j++) {
if(j < Math.floor(n/2) - i) {
string += " ";
}
if( j >= Math.floor(n/2) - i && j <= Math.floor(n/2) + i) {
string += k;
j < Math.floor(n/2) ? k++ : k--
}
if(j > Math.floor(n/2) + i ) {
string += " ";
}
}
string += "\n";
}
console.log(string);
}
p(7);

Shorter Way of Prinitng Triangle Patterns JavaScript

I'm trying to find a shorter way to print a right-triangle and an isosceles triangle without using that many nested loops. Please help, here is my code so far, it works I just have trouble finding a shorter way to write it:
//right-triangle
let triangleStr = "";
let num = 5;
for (let i = 1; i <= num; i++) {
for (let j = 0; j < num - i; j++) {
triangleStr += " ";
}
for (let k = 0; k < i; k++) {
triangleStr += "*";
}
triangleStr += "\n";
}
console.log(triangleStr);
//isosceles-triangle
triangleString = "";
for (let i = 1; i <= num; i++) {
for (let j = 1; j <= num - i; j++) {
triangleString += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
triangleString += "*";
}
triangleString += "\n";
}
console.log(triangleString);
If you want to use only one loop, you can use repeat() method in ECMAScript6 or higher.
For Right triangle:
const line = 5;
let rightTriangle = "";
for (let l = 1; l <= line ; l++) {
const indent = line-l;
rightTriangle += `${" ".repeat(indent)}${"*".repeat(l)}${"\n"}`;
}
console.log(rightTriangle);
For Isosceles triangle:
const line = 5;
let isoscelesTriangle = "";
for (let l = 1; l <= line ; l++) {
const indent = line - l;
isoscelesTriangle += `${" ".repeat(indent)}${"*".repeat(2*l-1)}${"\n"}`;
}
console.log(isoscelesTriangle);

Looping javascript multi array

var size = ['s','m'];
var color = ['red','blue','black'];
var material = ['cotton','linen'];
i want result like :
array("s,red,cotton","s,red,linen","s,blue,cotton","s,blue,linen","s,black,cotton","s,black,linen");
array("m,red,cotton","m,red,linen","m,blue,cotton","m,blue,linen","m,black,cotton","m,black,linen");
would you like to help me please use javascript or jquery. Thank you :)
Three loops for each array. Just loop through every array, and append to new array.
var size = ['s','m'];
var color = ['red','blue','black'];
var material = ['cotton','linen'];
var arrayMaterials = []
for (var i = 0; i < size.length; i++) {
for (var j = 0; j < color.length; j++) {
for (var k = 0; k < material.length; k++) {
arrayMaterials.push(size[i] + "," + color[j] + "," + material[k]);
}
}
}
console.log(arrayMaterials);
As said in Comment you need 3 loops
var size = ['s','m'];
var color = ['red','blue','black'];
var material = ['cotton','linen'];
for (var a = 0; a < size.length; a++) {
for (var b = 0; b < color.length; b++) {
for (var c = 0; c < material.length; c++) {
console.log(size[a] + " , " + color[b] + " , " + material[c]);
}
}
}

How to print a star pattern?

I want to print the following star pattern using JavaScript:
*****
***
*
***
*****
And I am trying this code, where I am getting off a triangle:
for (i = 3; i >= 1; i--) {
/* Printing spaces */
for (j = 0; j <= 3 - i; j++) {
document.write("&nbsp");
}
/* Printing stars */
k = 0;
while (k != (2 * i - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
}
try this code
after i==1 create new pattern inside it
for (i = 3; i >= 1; i--) {
/* Printing spaces */
for (j = 0; j <= 3 - i; j++) {
document.write("&nbsp");
}
/* Printing stars */
k = 0;
while (k != (2 * i - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
if (i == 1) {
for (t = 2; t <= 3; t++) {
for (j = 0; j <= 3 - t; j++) {
document.write("&nbsp");
}
/* Printing stars */
k = 0;
while (k != (2 * t - 1)) {
document.write("*");
k++;
}
document.write("<br/>")
}
}
}
Try this code:-
In this we use outer loop and inner loop.
var i, j;
//outer loop
for(i=1; i <= 5; i++)
{
//inner loop
for(j=1; j<=i; j++)
{
document.write('*');
}
document.write('<br/>');
}
Iteration:-
i j(Repeat)
1 1
2 2
3 3
4 4
5 5
const n = 5
for (var i = 0; i < n; i++) {
var star = '';
for (var j = 0; j < n - i; j++) {
star += ' ';
}
for (var k = 0; k < i; k++) {
star += '* ';
}
console.log(star);
}
for (var i = 0; i < n; i++) {
var star = '';
for (var b = 0; b < i; b++) {
star += ' ';
}
for (var a = 0; a < n - i; a++) {
star += '* ';
}
console.log(star);
}

Categories