Javascript nested array and setting the variable [closed] - javascript

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.

Related

I am not getting proper output with nested for-loop in javascript [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 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);
}

Javascript Question: Sum of all odd numbers to 100 [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 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);

JavaScript for-loop execution [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 wrote a program to have that particular output in javascript:
i=2 j=1
i=3 j=2
i=1 j=3
i=2 j=1
i=3 j=2
i=1 j=3
and so on...
But the following code never results in the output I need.
c=2;
for (i=c; i<=3; i++){
alert(i);
if(i==1){
j=3;
}else{
j=j-1;
}
alert(j);
if(i==3){
c=1;
continue;
}
}
That's seems to an easy problem : you just have to count 1 by 1 with 2 variables (i and j), the differences is than you don't start at 1 for both.
You need to increment j and i at the same time.
// this for not making infinite loop
var nb_loop=0;
var max_loop=10;
var j=0;
for (var i=2; i<=3 ; i++){
nb_loop++;
j++;
console.log("i="+i+", j="+j); // or alert if you want
if (j>=3)
j=0;
if (i>=3)
i=0;
if (nb_loop>max_loop)
break;
}
Note: a while loop may be better than the "for"
I don't know if u need to use this, i reproduce your output (see console)
var c=1,
j = 2,
i = 0,
x = 2; // this a limit for loop to prevent infinite loop
for (c; c<=3; c++){
if(c == 1){
i = 2;
}else if(c == 2){
i = 3;
}else{
i = 1;
}
//alert("i="+i+" j="+c); uncomment to see alert
console.log("i="+i+" j="+c);
//you can change this condition to stop the loop where you need
if(c==3 && x != 0){
c = 1;
x--;
}
}
see this example, maybe can help:
http://jsfiddle.net/pvMby/1/

Javascript substring not working as expected [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 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]);
}

why does this loop only work once [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
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++)

Categories

Resources