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 />");
}
I am little bit stuck in the below logic.
var t = new Array(),
i = 1;
for (var s = 0; s < 7; s++) {
t[s] = [];
for (j = 0; j < 7; j++) {
t[s][j] = i;
if (i === 6) {
i = 0;
}
i++;
}
}
console.log(t);
In my tried logic i am getting array like below
0: [1,2,3,4,5,6,1]
1: [2,3,4,5,6,1,2]
2: [3,4,5,6,1,2,3]
3: [4,5,6,1,2,3,4]
4: [5,6,1,2,3,4,5]
5: [6,1,2,3,4,5,6]
6: [1,2,3,4,5,6,1]
But I want output
0: [1,2,3,4,5,6,0]
1: [2,3,4,5,6,0,1]
2: [3,4,5,6,0,1,2]
3: [4,5,6,0,1,2,3]
4: [5,6,0,1,2,3,4]
5: [6,0,1,2,3,4,5]
6: [0,1,2,3,4,5,6]
Looking for help.
You can achieve it by rotating array.
let len = 7;
let a = Array.from(Array(len).keys());
let ans = [];
for(let i = 0; i < len; i++){
a = a.slice(1,a.length).concat(a.slice(0,1));
ans.push(a);
}
console.log(ans);
I would solve it in a different approach - You are building a two dimensional array that each new element (or row) is shifted or incremented by one (6 is the max value)
So increment each value by one and store the result.
$column = [1,2,3,4,5,6,0];
$max = 6;
$rows = 7;
$result = [];
for ($i = 0; $i < $rows; $i++) {
$result[$i] = [...$column];
for ($j = 0; $j < $column.length; $j++) {
if ($column[$j] == $max) $column[$j] = 0;
else $column[$j]++;
}
}
console.log($result);
reason of your bug
when doing so for the case i == 6
i = 0
i++
You are incrementing i twice... (from 6 to 1 instead of from 6 to 0)
You can:
if (i === 6) {
i = 0;
} else {
i++;
}
or more elegantly
i = (i + 1) % 7
Now you want i to be valid before assignment
t[s][j] = i will be not good if i == 7
so modulo it before
t[s][j] = i % 7
i++
let t = []
let i = 1
for (let s = 0; s < 7; s++) {
t[s] = []
i = s + 1 // start of your row
for (let j = 0; j < 7; j++) {
t[s][j] = i % 7
++i
}
}
console.log(t.map(r=>r.join(',')).join('\n'))
finally you can shorten your code by using map
let t = Array(7).fill(0).map( (r, i) => {
return Array(7).fill(0).map( (c, j) => ((i + 1) + j) % 7)
})
console.log(t.map(r=>r.join(',')).join('\n'))
Another approach is to use modulo. Using k % n will make the number rotate to 0 with every n
Also, you don't need the variable i. You could calculate it using s + j + 1 instead.
function p(a)
{
let str="";
for(i of a)
{
for(j of i)
{
str += j + " ";
}
str += "\n";
}
console.log(str);
}
var t = new Array();
var arraySize = 7;
var rotateEvery = 7;
for (var s = 0; s < arraySize; s++) {
t[s] = [];
for (j = 0; j < arraySize; j++) {
t[s][j] = (s + j + 1) % rotateEvery;
}
}
p(t);
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
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(" ");
}
/* 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(" ");
}
/* 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(" ");
}
/* 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);
}
Using Javascript, I need to create a matrix table that looks like the matrix below:
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
The following code, to me, seems like that should be correct, however when I try to output arr[0][1] etc... no value show. Did I do something wrong in the following code:
var address = [
"...",
"....",
".....",
"......"
];
var arr = [];
for (i = 0; i < address.length; i++)
{
for (j = 0; j < address.length; j++)
{
//alert(i + " " + j);
if (i=j)
{
arr[i][j]=0;
}
else
{
arr[i][j]=1;
}
}
}
You have 2 problems in your script
for (i = 0; i < address.length; i++) {
//need to initialize arr[i] else `arr[i]` will return undefined
arr[i]= [];
for (j = 0; j < address.length; j++) {
//need == not =
if (i == j) {
arr[i][j] = 0;
} else {
arr[i][j] = 1;
}
}
}
console.log(arr)
Where the if..else can be replaced by a ternary operator
for (j = 0; j < address.length; j++) {
arr[i][j] = i == j ? 0 : 1;
}
Demo: Fiddle