Inclined pyramid pattern - javascript

I want to create a pyramid-pattern (on the side).
I've gotten the first part, but I need help to count the last part down.
So far I've got this:
var out = document.getElementById("out");
for (var i = 0; i < 20; i++ ){
if ( i <= 10){
for ( var j = 0; j < i; j++ ){
out.innerHTML += "*";
}
} else if (i > 10){
for (var j = 9; j > 0; j--){
out.innerHTML += "*";
}
}
out.innerHTML += "<br>";
}
<p id="out"></p>

You can run a loop from 19-i to 0, and while i increments further the value of 19-i gets smaller and smaller:
var out = document.getElementById("out");
for (var i = 0; i < 20; i++ ){
if ( i <= 10){
for ( var j = 0; j < i; j++ ){
out.innerHTML += "*";
}
} else if (i > 10){
for ( var j = 19-i; j >= 0; j--){ // <-- run loop so that number of iterations gets lower and lower
out.innerHTML += "*";
}
}
out.innerHTML += "<br>";
}
<p id="out"></p>

Simple solution to draw Pyramid shape based on the given steps/count:
var steps = 5, outputStr = "", isTriangle = false;
var newLine = "<br />"; // "\n" for String, <br /> for element break line.
for (var i = 1; i <= steps; i++) {
// Forward
for (var j = 1; j <= i; j++) {
outputStr += i;
}
outputStr += newLine;
if (i == steps && !isTriangle) {
// Backward - After completing Forward
var k = i - 1;
while (k > 0) {
for (var j = k; j > 0; j--) {
outputStr += k;
}
k--;
outputStr += newLine;
}
}
}
var out = document.getElementById("out");
out.innerHTML = outputStr;
<p id="out"></p>
for java see: Draw Pyramid

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

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 />");
}

Javascript Array manipulation and looping

Output of the following code is given after the program. I need to iterate j value in the following order (1,2,3,4),(2,3,4,1),(3,4,1,2),(4,1,2,3) but it is in order of(1,2,3,4),(2,3,4),(3,4),(4) . Any help is much appreciated
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
for (i=0; i < n; i++){
for (j=i+1; j<n ;j++){
document.write("</br>");
document.write("i is "+i+" j is "+j);
var h = (RRIntervalArrayy[j] - RRIntervalArrayy[i]);
document.write("</br>");
if(h < 0.12){
validRRIntervalCount++;
}
document.write(h);
if(j==(n-1)){
document.write("</br>");
document.write(validRRIntervalCount)
break;
}
}
validRRIntervalCount = 0;
document.write("</br>");
}
output
i is 0 j is 1
0.030000000000000027
i is 0 j is 2
-0.21999999999999997
i is 0 j is 3
1.92
i is 0 j is 4
0.030000000000000027
3
i is 1 j is 2
-0.25
i is 1 j is 3
1.8900000000000001
i is 1 j is 4
0
2
i is 2 j is 3
2.14
i is 2 j is 4
0.25
0
i is 3 j is 4
-1.8900000000000001
1
You need to set j as 0 when it comes to the end of the array. And the double loop is useless.
var i=0;
var j=i;
for(i=0; i<arrayLength ; i++){
j++;
if(j==arrayLength){
j=0;
}
//Do your stuff
}
Instead of managing complex looping, you can just shift your element from start to end.
RRIntervalArrayy.push(RRIntervalArrayy.shift());
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
for (i=0; i < n; i++){
document.write("--------------------------------");
document.write("</br>");
for (j=0; j<n ;j++){
document.write("</br>");
document.write("array element is:" + RRIntervalArrayy[j] );
var h = (RRIntervalArrayy[j] - RRIntervalArrayy[i]);
document.write("</br>");
if(h < 0.12){
validRRIntervalCount++;
}
document.write(h);
if(j==(n-1)){
document.write("</br>");
document.write(validRRIntervalCount)
break;
}
}
RRIntervalArrayy.push(RRIntervalArrayy.shift());
validRRIntervalCount = 0;
document.write("</br>");
}
Just to print in the order you need, try this:
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
var revert = 0;
for (i=0; i < n; i++){
revert = 0;
for (j=i+1; j< n ;j++){
if(revert == 1 && j == i){
break;
}else if(revert == 1 && j != i){
document.write(j + 1);
continue;
}
document.write(j);
if(j==(n-1)){
if(revert == 0){
j = -1;
revert = 1;
}
}
}
validRRIntervalCount = 0;
document.write("</br>");
}
To strictly do what you want:
for (i=0; i < n; i++){
var j = i + 1
var count = 0
while (count < n-1){
if (j == n)
j = 1
// do your stuff
count++
j++
}
// do your stuff
}
Just a little bit of modulus magic can do he Job for you
var RRIntervalArrayDiff = [];
var validRRIntervalCount = 0;
var RRIntervalArrayy = [0.62, 0.65, 0.40, 2.54, 0.65];
var n = RRIntervalArrayy.length;
for (i = 0; i < n; i++) {
for (j = i; j < n + i; j++) {
var newJ = j % (n) + 1;
//use newJ instead of j in your calculations
}
}

Categories