How to add spaces to for loop output in javascript? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I want to add one space in between each outcome in this for loop,
for (var i = -52; i <= 1066; i++) {
document.write(i)
}

You can add to do it
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
Or just add to space in the write
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}

this should help you out.
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
or
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}

Browsers are designed to ignore whitespace such as spaces, typing in multiple ' ' (spaces) will be reduced to a single space. If you're looking to add multiple spaces in Javascript the easiest way is to use &nbsp. Such tags are called entities and numerous characters can be printed using entities. More can be found here
for(var i = 0;i < n;i++)
{
document.write("&nbsp")
}

for (var i = 52; i <= 1066; i++) {
document.write(i+'\t') }

Try this:
let result = [];
for (let i = -52; i <= 1066; i++) {
result.push(i);
}
document.write(result.join(" "));
Suggestion: avoid using document.write at best, especially in a big for loop.

Related

Array in each position successively Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I've got trouble with arrays. I'm trying to iterate elements for each position in the array using javascript.
so, I need to do that on the first position, be a value [1]. On the second position, [2, 3, 4]. on the third position, [5,6,7,8,9] and successively two by two.
My attempt was did a for loop:
for (let i = 1; i <= 5; i++) {
for (let j = 1; j <= 5; j++) {
console.log(`${i} ${j}`)
}
}
But while looping, the indices (i) repeat with (j).
What to do?
Try this code.
var counting = 1;
for (let i = 1; i <= 5; i = i + 2) {
var str = "";
for (let j = 1; j <= i; j++) {
str += `${counting} `;
counting++;
}
console.log(str);
}

Javascript for loop hashtag [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Hey I am trying to print out #, by increasing the amount of # for each line. like this:
#
##
###
####
#####
Here is the for loop that I have tried to work this out with:
var printout = "";
for(var i=0;i<5;i++) {
printout+= "#" + " <br>";
You can add one more variable for incrementing #
var printout = "", a = '';
for (var i = 0; i < 5; i++) {
printout += (a += '#') + " <br>";
}
document.body.innerHTML += printout;
If you're just trying to print them:
for (var i=0; i < 5; i++) {
console.log("#".repeat(i+1))
}
Check this out, output will be in HTML.
var printout = "";
for(var i=0;i<5;i++) {
for(var j=0; j<=i; j++)
{
printout += "#";
}
printout += " <br>";
}
var pvar = document.getElementById("check");
pvar.innerHTML = printout;
<p id="check">
</p>

Javascript Multiplication Table Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have been trying to do this for a while and I can't quite get it. I basically want a 2d array that is a multiplication table. So if I reference multTable[5][5] I would get 25. I have found scripts for a table that is printed but not one for an array. This is the best code I have so far.
var multTable;
for(var v = 0; i<13; v++) {
for(var i = 0; i<13; i++) {
multTable[v][i]=i*v
}
}
Javascript doesn't have built-in type of multi-dimensional array. So you can't declare multTable first and then directly use multTable[v][i]=i*v. You need to create an array of arrays.
var multTable = [];
for (var v = 0; v < 13; v++) {
multTable.push([]);
for (var i = 0; i < 13; i++) {
multTable[v].push(i * v);
}
}
multTable[5][5] // 25
Or alternatively, you can use object.
var multTable = {};
for (var v = 0; v < 13; v++) {
multTable[v] = {};
for (var i = 0; i < 13; i++) {
multTable[v][i] = i * v;
}
}
multTable[5][5] // 25
I'm not exactly sure what the question is, but it looks like your first for loop exit condition is using the wrong variable. Try the following:
var multTable;
for(var v = 0; v<13; v++) {
for(var i = 0; i<13; i++) {
multTable[v][i]=i*v
}
}

A quite weird array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Here is my problem, I'm programming a game and I need to use a very simple array, the problem is that I create it, everything's ok, and the next line, the values of an entire line in the array change and I don't understand why, here is my code :
var ciblesPossibles = new Array();
for (var i = 0; i < 2; i++) {
for (var j = 0; j < 5; j++) {
ciblesPossibles[i,j] = 1 + portee(i, j, ID, pos, carte.effet.portee) + duBonCote(i, ID, carte.effet.ciblesLegales);
}
console.log('1/ ciblesPossibles['+i+',x] = ' + ciblesPossibles[i,0] + ciblesPossibles[i,1] + ciblesPossibles[i,2] + ciblesPossibles[i,3] + ciblesPossibles[i,4]);
}
for (i = 0; i < 2; i++) {
console.log('2/ ciblesPossibles['+i+',x] = ' + ciblesPossibles[i,0] + ciblesPossibles[i,1] + ciblesPossibles[i,2] + ciblesPossibles[i,3] + ciblesPossibles[i,4]);
}
var max = maxTab(ciblesPossibles);
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
ciblesPossibles[i,j] = Math.floor(ciblesPossibles[i,j] / max);
console.log(ciblesPossibles[i,j]);
}
}
portee() and duBonCote() are two functions which return just 1 or 0.
When I'm at the console.log('/1...'), I have something like 33222 and 22211 (it's what I want), but when I'm at the console.log('/2...'), I have 22211 and 22211 ... What can make the first line change in my array ?
Regards
Two dimensional arrays are accessed as a[i][j], not a[i,j].
The latter will be treated as a use of the comma operator, and evaluates to just a[j], i.e. a one-dimensional matrix.
You'll be wanting something more like:
var ciblesPossibles = []; // create array to hold rows - NB: not "new Array():
for (var i = 0; i < 2; i++) {
ciblesPossibles[i] = []; // create the individual row
for (var j = 0; j < 5; j++) {
ciblesPossibles[i][j] = ...
}
}

How to write this piece of code in jQuery? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to implement this javascript code in jquery, please!
var html = '';
for (i = 0; i < 2; i++) {
for (var j = 0; j < 7; j++) {
var num = Math.floor(Math.random()*39)+1;
html += num + ' ';
}
html += '<br>';
}
document.getElementById('rows').innerHTML = html;
There is nothing wrong with the code you posted and there is not much difference between the straight javascript version and a version written in jQuery. The only different is the last line, where you select the rows object. In jQuery you get the object using $('#rows').
Here you go:
var html = '';
for (i = 0; i < 2; i++) {
for (var j = 0; j < 7; j++) {
var num = Math.floor(Math.random()*39)+1;
html += num + ' ';
}
html += '<br>';
}
$('#rows').html(html);

Categories