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] = ...
}
}
Related
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);
}
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 2 years ago.
Improve this question
I have a function generating color id's:
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length)
color += str[index]
}
return '#' + color
}
And then I'm using another function, as I want to have an array of six generated colors:
const colorArray= ()=> {
let colors = []
for (let i=0; i>=6; i++ ){
colors.push(hexaColor[i])
console.log(colors)
}
return colors;
}
console.log(colorArray())
however, what I see in a console is just an empty array. What am I doing wrong? Any help appreciated.
You have a code error
The loop did not run at all
The condition will never be meti>=6
Change the condition at the top of the loop
for (let i=0; i>=6; i++ ){
This is how it works
for (let i=0; i<=6; i++ ){
also this line fix to
colors.push(hexaColor()[i])
The issue is that in your loop you said when i >= 6, but it's i <=6 because i starts from 0.
const hexaColor = () => {
let str = '0123456789abcdef'
let color = ''
for (let i = 0; i < 6; i++) {
let index = Math.floor(Math.random() * str.length)
color += str[index]
}
return '#' + color;
}
const colorArray = ()=> {
let colors = []
for (let i=0; i<=6; i++ ){
colors.push(hexaColor());
}
return colors;
}
console.log(colorArray())
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 5 years ago.
Improve this question
When I try to execute this it takes index from [1] position but not zero. How to take from zero.
function mul(){
var temp = 1;
for (i=1 ; i < arguments.length; i++){
temp = temp * arguments[i];
}
return temp;
}
var list = mul(2,2,2,2,2);
alert(list);
This is because var i is set to 1. arguments is an array-like object and it also start from 0 index. In your case your are starting from 1st index.
function mul() {
var temp = 1;
for (i = 0; i < arguments.length; i++) {
temp = temp * arguments[i];
}
return temp;
}
var list = mul(2, 2, 2, 2, 2);
console.log(list);
According to airbnb style it is better to use rest syntax instead of arguments object. Beside i in the for loop has not defined with var keyword. In that case it will be global variable & will be accessible from other function. So defining a variable with var keyword will bound its scope to current function only. You can also explore let
function mul(...args) {
var temp = 1;
for (var i = 0; i < args.length; i++) {
temp = temp * args[i];
}
return temp;
}
var list = mul(2, 2, 2, 2, 2);
console.log(list);
The line 3 for loop:
for (i=0 ; i < arguments.length; i++){...
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
}
}
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
I have a function meant to construct an array full of objects using a for-loop. However, my debugger jumps over the for-loop for some reason, and i don't know why. here is the function:
function objArrCon() { //object array constructor
var arr = [];
var len = 9;
for (var i = 2; i === len; i++) {
arr.push({
name: i,
count: 0
});
}
return arr;
}
This line
for (var i = 2; i === len; i++) {
Should be
for (var i = 2; i <= len; i++) {
change you for loop like this. this my helps you
function objArrCon() { //object array constructor
var arr = [];
var len = 9;
for (var i = 2; i <= len; i++) { // change this line
arr.push({
name: i,
count: 0
});
}
return arr;
}