JavaScript nested for loops to display numbers without HTML - javascript

I need to display a number range between 1 and 30 in the console, but in a specific way. While I've figured out how to get the range using a for loop, I have not figured out how to display the numbers in the console like in the image shown below where each * represents a number 1-30. I need numbers 1-30 to be displayed 7 rows across, and 5 rows down, without using HTML tables.
example
My code, to display the number range, is as follows:
for (var i = 1; i <= 5; i++)
{
var output = " ";
for (var j = 0; j <= 7; j += 1)
{
output += "*" + "\t";
}
console.log(output);
}
So far I have tried adding a third nested for loop, but there will be 5 iterations of 1 - 30 displayed 7 times. I have also tried using an array of 1-30, a while loop, an if statement, and have tried adding or multiplying variables with similar results.
I can't help but feel like I am approaching this the wrong way. I am considering using an array and having the inner for loop display each index of the array, but I am not sure if JavaScript has the ability to move to the next index after printing the previous index (1 then 2 then 3, etc) in the way in which I need it to as shown in the image.

This code works
var output="";var count = 0;
for (var i = 1; i <= 5; i++)
{
//output = " ";
for (var j = 0; j < 7; j += 1)
{
output +=(count+1)+"\t";count ++;
}output+="\n";
//document.write(output);
}
console.log(output);
Output:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
Problem in Question:
Question States -Number range between 1 and 30 in the console but specified order of
matrix is 7*5 which is equal to 35 not 30 which was wrongly mentioned
in the questiion.
Screenshot

As indicated by #torazaburo the OP is looking to populate numbers as opposed to [ * ] . Though SaiKiranUppu had a satisfactory answer and should be awarded the upvote, I wanted to offer another solution:
JS:
function matrix(r, c) {
var n = '';
var x = 1;
for(var i = 1; i <= r; i++) {
for(var j = 0; j < c; j++) {
n += x + '\t';
x++;
}
n += '\n';
}
console.log(n);
};
matrix(5, 7);

You can just do this. I do it only for your..
var lastJ = 1;
for (var i = 1; i <= 6; i++) {
var output = " ";
var j = lastJ ;
var totalLoop = j+4;
for (j = lastJ; j <= totalLoop; j++) {
output += j + "\t";
}
console.log(output);
lastJ = j;
}
output is like this
console output in image
If you want calendar like output use this
var lastJ = 1;
for (var i = 1; i <= 5; i++) {
var output = " ";
var j = lastJ ;
var totalLoop = j+6;
for (j = lastJ;j <= totalLoop; j++) {
if(j<32){
output += j + "\t";
}else{
output += 0 + "\t";
}
}
console.log(output);
lastJ = j;
}
output is like this
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 0 0 0 0

First of all, Welcome to SO!
A newline element after every outer loop should do the trick. Something like this.
for (var i = 1; i <= 5; i++)
{
var output = "";
for (var j = 1; j <= 7; j += 1)
{
output += "*" + "\t";
}
output += "\n";
console.log(output);
}
Edit: Realised OP wanted numbers and not *s. A simple modification to the code makes this unbelievably easy.
var output = "";
for (var i = 1; i <= 5; i++)
{
for (var j = 1; j <= 7; j += 1)
{
output += i*j + "\t";
}
output += "\n";
}
console.log(output);
}
Output - Chrome Developer Tools Screenshot

Related

Print a list of number using While loop and Do While loop

I am trying to implement the while and do while loop without using an actual array to print an list of number. Here is my code and the output I want.
My code
console.log("While Loop")
let j = 0;
while(j < 5){
j += 1;
console.log (j);
}
console.log("Do While Loop")
var i = 0; //set varible i = 0
do {
i += 1; //return i + 1 to i
console.log (i);
}
while (i < 5); //where i suppose to less than 5
Actual Output I want:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
There is two things here:
You need an inner loop to create the countdown of numbers.
If you are only using JavaScript, you need to form a string with your output and then use console.log() to print. This is because console.log() will append a newline.
The example below will print the first half of the triangle.
console.log("While Loop")
let j = 1;
while (j <= 5) {
let k = 1;
let str = "";
while (k <= j) {
str += k + " ";
k++;
}
j += 1;
console.log(str);
}
If you are using Node.js you can use process.stdout.write to print to the same line and then explicitly write the newline character when needed.
console.log("While Loop")
let j = 1;
while (j <= 5) {
let k = 1;
while (k <= j) {
process.stdout.write(k + " ");
k++;
}
j += 1;
process.stdout.write("\n");
}

How to write a Nested for loops for numbers starting larger than 0

I'm trying to write a Nested for loops for my school assignment that will print this, exactly like this, you cant change the starting number
10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
so far I wrote this
var i, col, row, number, text;
text = ' ' ;
number = 10;
for(col = 0; col < 10; col++) {
for (row = 0; row <= col; row++) {
text+=row;
text+="<br>";
console.log(text)
}
}
The problem is that it starts from number 0, not 10. I also need to use a for loops to do it.
To start the for loop at 1 simply replace the for(col = 0 with for(col = 1
For the box:
let n = 5
for(let i = 0; i < n; i++) {
for(let j = 0; j < n; j++) {
if(i === 0 || i === n-1){
console.log("o")
} else if (j === 0 || j === n-1) {
console.log("o")
}
}
}
should do it (I have not tested it)
You could take a growing offset. This values starts with 10 and increments for each line with the item count of one line.
The you need another loop to collect the values for a line. After collecting show the values.
var lines = 9,
offset = 10,
length = 1;
while (length <= lines) {
console.log(...Array.from({ length }, (_, i) => offset + i));
offset += length++;
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
With output as html.
var lines = 9,
offset = 10,
length = 1,
element = document.getElementById("output");
while (length <= lines) {
element.innerHTML += Array.from({ length }, (_, i) => offset + i).join(' ') + '\n';
offset += length++;
}
<pre id="output"></pre>
a lot of trial and error with the maths but it works
let startnumber = 10;
let iterations = 10;
for (let i = 1; i < iterations; i++) {
let temp = "";
for (let j = i; j > 0; j--) {
temp += (j-Math.floor((i*i)/2+i/2))*-1+startnumber + " ";
}
console.log(temp);
}

How to console.log number in the following format?

I'm trying to display numbers in the following format can you please tell me what is wrong?
Can not use div. As i'm printing this in the console.
I for n = 5:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
I have tried the following code:
n = 10
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= i; j++) {
console.log("0" + j + " ");
}
console.log("<br />");
}
n = 10
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= i; j++) {
console.log("0" + j + " ");
}
console.log("<br />");
}
But it displays something like this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
Here is the code for your Task. It takes numbers from 1 to declared. Also puts some space at the beginning of each row. You need to declare a string at the beginning of each row and then add all elements to it. When all loops are done, you can print it in console with console.log.
var n = 10 // Declaring number of rows
for (var i = 1; i <= n; i++) {
var row = ''; //declaring text variable for current row
for (var x = n - i; x >= 1; x--) {
row += ' '; //adding spaces in begining of the row
}
for (var j = 1; j <= i; j++) {
row += ' ' + j; // numbers which increase up to middle
}
for (var k = i - 1; k >= 1; k--) {
row += ' ' + k; // adding rest of the numbers to the row
}
console.log(row); //displaying whole row
}
for the algorithm point of view you have the forward loop but you missed the downward loop
adding it more the indentation from your first version of the question :
var n = 10
for (var i = 1; i <= n; i++) {
for (var j = 0; j < n - i; j++) {
document.write(" ");
}
for (var j = 1; j <= i; j++) {
document.write(" " + j + " ");
}
for (var j = i - 1; j >= 1; j--) {
document.write(" " + j + " ");
}
document.write("<br />");
}
P.S. It is the very first time I write code in javascript ;-)

Need help to fix a snippet from my math grid maze solver

The idea behind the following code is to test if any number between 0 and 13 + any other number equals 13. If one does both numbers should be saved to a different array but on the same index. So i should have all possible combinations to reach 13 in 2 arrays. But when i run my code I only get 2 combinations which are 0+13 and 13+0. Here is the code:
var number1 = [];
var number2 = [];
var index = 0;
var i = 0;
var j = 0;
//Tests if i + j (from the loop) add up to 13
var test = function(i, j) {
if (i + j === 13) {
number1[index] = i;
number2[index] = j;
index =+ 1;
}
}
//1st loop generates i from 0 to 13 in 0.5 step.
for (i = 0; i < 13.5; i += 0.5) {
//same for j, this number should test with i every loop
for (j = 0; j < 13.5; j += 0.5) {
test(i, j);
}
}
//outputs the 2 arrays, the matching numbers should be stored in
for (i = 0; i < number1.length; i++) {
console.log(number1[i]);
console.log(number2[i]);
}
Change index =+ 1 to index += 1
Then index =+ 1 sets the index to 1 it does not increment it by 1 (as you want)
See Expressions and operators: Assignment operators MDN

Using two nested loops, display in the window the following line: 11 12 13 21 22 23 31 32 33 JavaScript

I have a problem with a nested for loop and not be able to resolve the task.
Could you please help me find a solution?
Please see the code below. It is what i was able to do.
<script>
let str='';
for ( let i =11; i<=33; i+=10){
str=''
for (let j =1; j<=3; j++){
document.write(i+' ')
}
}
</script>
My result is wrong : 11 11 11 21 21 21 31 31 31
For your sequence: Each number is the sum of i and j, and j should iterate from 0 to 2;
suppose i = 11
i + 0 = 11
i + 1 = 12
i + 2 = 13
Where j is 0,1,2
Working solution:
let str='';
for ( let i =11; i<=33; i+=10){
str=''
for (let j =0; j<=2; j++){
const num = i + j;
document.write(num + ' ')
}
}
Can try this for i and j both between 1-3.
let str = '';
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
document.write(i.toString() + j.toString() + ' ')
}
}

Categories

Resources