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 can't work out why this only loops through the array once in JavaScript. It should log the second nested array as well.
JSFiddle below and code below;
http://jsfiddle.net/HJfbT/
b = [["one", "is"],
["two", "is"]];
for (var i = 0; i < b.length; i++) {
for (var x = 0; x < b[x].length; x++) {
console.log(b[i][x]);
}
}
Because you have a typo:
// --- should be i ---v
for (var x = 0; x < b[x].length; x++) {
DEMO: http://jsfiddle.net/HJfbT/1/
Use b[i].length in the second loop.
I think is because the inner loop have the running condition with an error.
Is:
for (var x = 0; x < b[x].length; x++)
but must be:
for (var x = 0; x < b[i].length; x++)
Related
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 3 years ago.
Improve this question
I'm supposed to get the multiplication value of the multi-dimensional array. but I am getting '1' as output whatever values being changed in array.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i];j++){
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You need to check arr[i].length in the j loop.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i].length;j++){ // you need to check arr[i].length here
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You can simply do this in two lines.
function arrayMultiplyer(arr){
let flattenedArray = arr.flat();
return flattenedArray.reduce((x, y) => x * y);
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
An alternative solution using .reduce()
function arrayMultiplier(arr) {
return arr.reduce((tot,arr2) =>
arr2.reduce((subTot, n) =>
subTot * n
, tot)
, 1);
}
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 4 years ago.
Improve this question
I am trying to find out the sum off all odd numbers from 1 to 100 using a for loop. Here is my code so far:
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 1);
sum += i;
}
console.log(sum);
I thought maybe using a while loop maybe the solution, but whenever I try it, I always get an infinite loop error. So I tried switching back to a for loop, but the answer does not come out right. I believe I am looping through each number instead of every odd number. Is my "if" condition not correct? Any help is appreciated.
Thank You
You have a ; just after your if statement, so the next line is not going to get executed.
Just removing the ; should work:
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 1)
sum += i;
}
console.log(sum);
use step 2
var sum = 0;
for (var i = 1; i < 100; i+=2) {
sum += i;
}
console.log(sum);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
hi guys i tryed to create an empty array to fill it later. I post my code so you can help me. XD
"use strict"
var array = new array(6);
for(var i = 0; i <= 5; i++){
do {
var number = prompt("Put the element" + (i+1), 0);
}while(isNaN(number));
arr.push(number);
};
write.data(array);
It appears you may have copy and pasted some code without understanding what it is doing.
You are trying to push to an array, but you've declared your array as array, but trying to push to arr. Which is why it's not working.
var arr = new Array();
for(var i = 0; i <= 5; i++){
arr.push(i + 1);
}
console.log(arr);
alert(arr);
EDIT * You actually don't even need to declare the new Array(6), you can just use new Array() to push. However, if you would like to declare the size, you can do this instead.
var arr = new Array(6);
for(var i = 0; i <= 5; i++){
arr[i] = i + 1;
}
console.log(arr);
alert(arr);
You can also use this:
var arr = []; // create an empty object
for(var i = 0; i <= 5; i++){
arr.push(i + 1); // fill the object
}
console.log(arr);
alert(arr); // shows 1,2,3,4,5,6
This does what you are trying to achieve.
You can do it like
var a = new Array(6);
for(var i = 0;i < 6;i++){
a.push(i+1);
}
console.log(a);
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 5 years ago.
Improve this question
for (i = 0; i < length1; i++) {
for (j = 0; j < length2; j++) {
for (k = j; k < length2; k++) {}
}
}
I just wondered... in the last loop where I initialize k to be equal to j, would it cause problems for me later on?
I'm assuming every time j increments by 1, k is set to the new value of j and then increments up to length2
You are correct that for each iteration of the j-for-loop, the innermost loop will begin at J's current value and iterate up to length2-1. If this is what you want, shouldn't be a problem.
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 8 years ago.
Improve this question
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
alert(test.substring(i,1));
}
I expected each alert to return each letter of the alphabet individually.
Instead, the first 5 alerts displayed as follows. Why?
a
b
bc
bcd
bcde
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
console.log(test.substring(i,i+1));
}
actually, it's
substring(start, end)
not
substring(start, length)
unlike substr, which is indeed, substr(start, length)
If "start" is greater than "end", this method (substring) will swap the two arguments, meaning str.substring(1,4) == str.substring(4,1).
Use:
for(i = 0; i < test.length; i++) {
alert(test[i]);
}