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 ;-)
Related
I'm trying to create a Pascal triangle using the Fibonacci sequence. I´m looking for this output:
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
0 1 1 2 3 5 8
0 1 1 2 3 5
0 1 1 2 3
0 1 1 2
0 1 1
0 1
0
This is the code I have written so far. I managed to get the Fibonacci sequence running into a triangle but not the way I want.
function fiboP(n) {
let string = "";
let n1 = 0
let n2 = 1
for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
string += n1 + " ";
next_num = n1 + n2;
n1 = n2;
n2 = next_num;
}
string += "\n";
}
for (let i = 1; i <= n - 1; i++) {
for (let j = 0; j < n - i; j++) {
string += n1 + " ";
next_num = n2 - n1;
n2 = n1;
n1 = next_num;
}
string += "\n";
}
console.log(string)
}
fiboP(5)
Output:
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377
610 377 233 144
89 55 34
21 13
8
I would like to understand what I am missing here and if there is a cleaner and simpler way to produce the desired output.
If you reset your values when you go to next line, you should be able to generate the output you're looking for
function fiboP(n) {
let string = "";
for (let i = 1; i <= n; i++) {
let n1 = 0
let n2 = 1
for (let j = 0; j < i; j++) {
string += n1 + " ";
next_num = n1 + n2;
n1 = n2;
n2 = next_num;
}
string += "\n";
}
for (let i = 1; i <= n - 1; i++) {
let n1 = 0
let n2 = 1
for (let j = 0; j < n - i; j++) {
string += n1 + " ";
next_num = n2 + n1;
n2 = n1;
n1 = next_num;
}
string += "\n";
}
console.log(string)
}
fiboP(7)
As an improvement i will suggest finding fibonacci sequence once and then just using these values to create this triangle.
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");
}
I have following java script code segments to print less than 5 numbers between 1-10.
<script type="text/javascript">
for (let i = 1; i < 10; i++) {
if (i < 5) {
document.write('The Number is' + i + '<br>');
}
}
</script>
Then I need print this using while loop. how can I do this?
Try this:
let i = 1;
while (i < 10) {
if(i < 5) {
document.write('The Number is ' + i + '<br>');
}
i++;
}
By using while loop, write a program to print the numbers from 1 to 10.
let i = 1;
while (i <= 10) { //while (i < 11) {
console.log(i);
i++;
}
Output :
1
2
3
4
5
6
7
8
9
10
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
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() + ' ')
}
}