I need a 2 counter, i & j that output the following counters for each iteratation:
0,1,2,3,4,1,2,3,4,2,3,4,3,4
Counter variable sequence to achieve the above when output to console.log is as follows:
i,j,j,j,j,i,j,j,j,i,j,j,i,j
With each iteration, when i increments, j must start it's incrementation at i + 1 and must complete it's loop for each incrementation of i.
Each counter value will be used to present option from an array for comparison with one another.
I've tried using a nested for loop and it has not worked. I am also calling a function containing the second loop with some success but want to know if there is a more elegant way to accomplish this.
function createValues() {
for (i = 0; i < 9; i++) {
var counter_i = i;
decreasingLoop(counter_i);
}
}
var k = 1; // counter for inner values loop
function decreasingLoop(get_i) {
for (j = k; j < 10; j++) {
// functions using the counters are entered here.
if (j == 9) { // increments k so that the next time the counter runs it starts at +1 from previous iteration)
k++;
}
}
j = k;
}
I am getting the desired results but want to know if there is a simpler way to achieve the desired outcome.
for(var i = 0; i< 4; i++){
console.log(i)
for(var j = i+1; j<= 4; j++){
console.log(j)
}
}
If you write out your desired output in a different format, the solution may become a little clearer:
i: 0
j: 1,2,3,4
i: 1
j: 2,3,4
i: 2
j: 3,4
i: 3
j: 4
As you can see from the above example, i increases linearly, and only takes the values of 0 through to 3. Each time i takes a new value, j loops from i+1 up to 4. Using this idea, you can create an outer for loop for i (which loops from 0 to 3) and an inner for loop for j which starts at i+1 and finished when j is equal to 4:
for(let i = 0; i <= 3; i++) {
console.log(i);
for(let j = i+1; j <= 4; j++) {
console.log(j);
}
}
Try following nested loop - notice that in second loop initial for value is j=i+1
let n=4, m=5;
for(let i=0; i<n; i++) for(let j=i+1; j<m; j++) {
console.log({i,j});
}
Related
this is the code in question, I'd like to know why the inside loop variable counts from 0,1,2 and the outside variable counts from 0,1,2,3
let i = 0;
for (i = 0; i < 3; i++) {
console.log(i);
}
console.log(i);
i++ is incrementing i each time until i == 3, then it breaks out of the for loop. So your console.log(i) inside the loop runs until it hits 3 then the console.log(i) outside the loop gets run once - after i is already equal to 3
let i = 0; at this level, your variable i is worth 0
The variable i is incremented from 0 to 2. so at the end of the loop the variable i = 2 console.log(i) will print 2
let i = 0;
for(i = 0; i < 3; i++){
console.log(i) // this will print 0, 1, 2
}
console.log(i) // this will print 2
I added "break" in order to analyze the code. I'm pretty much a beginner, and
trying really hard to understand why this code document writes "135 ".
As long as i is smaller than arr.length, add one to i, that one is clear.
The second line refers to arr[i].length, I don't know what that is even after researching.
Maybe the length of arr[0], arr[1] and arr[2] = arr[i].length, so 2?
then the execution of the inner for loop I simply don't understand.
var arr = [
[1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
document.write(arr[i][j]);
break;
}
}
So heres the flow:
var arr = [
[1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
// Inside this first loop we are dealing with an
// item in arr (e.g., [1,2])
for (var j=0; j < arr[i].length; j++) {
// Inside this loop we are dealing with an item
// from the previous loop (e.g, 1)
// We write the one and then exit the inner loop
// and move on to the next set of pairs.
document.write(arr[i][j]);
break;
}
}
Sometimes it's easier to understand using variables. This is equivalent:
var arr = [
[1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
var pair = arr[i];
for (var j=0; j < pair.length; j++) {
var num = pair[j];
document.write(num);
break;
}
}
The break statement breaks the loop and continues executing the code after the loop. (https://www.w3schools.com/js/js_break.asp)
In this case, the 'break' only jumps out from the inner loop.
The outer loop will be executed 3 times.
You can simulate all the executions like this
i=0;
j=0; j<arr[0].length;
document.write(arr[0][0]); // output 1
break; // break inner loop
i=1;
j=0; j<arr[1].length;
document.write(arr[1][0]); // output 3
break; // break inner loop
i=2;
j=0; j<arr[2].length;
document.write(arr[2][0]); // output 5
break; // break inner loop
```
Can you please explain to my why i get to different results;
This is the finished code and the result I wished:
function largest_of_arrs(arr){
var largest_arr = [];
var holder;
var max = 1;
for(var i = 0; i < arr.length; i++){
var sum = 0;
for(var j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if(sum > max) {
max = sum;
largest_arr = arr[i];
}
}
}
return largest_arr;
}
var array_1 = [[1,2,3,4,5],[1,2,2,3,1],[11,12,23,45,88],[20,20,5,5,1]];
largest_of_arrs(array_1);
This gives me the wished result: [11,12,23,45,88]
But when i declare the sum variable outside the for loop like this
function largest_of_arrs(arr){
var largest_arr = [];
var holder;
var max = 1;
var sum = 0;
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].length; j++){
sum += arr[i][j];
if(sum > max) {
max = sum;
largest_arr = arr[i];
}
}
}
return largest_arr;
}
var array_1 = [[1,2,3,4,5],[1,2,2,3,1],[11,12,23,45,88],[20,20,5,5,1]];
largest_of_arrs(array_1);
The result is: [20,20,5,5,1]
Can you please explain to me why ?, and how to scope here works?
if possible please with pictures (graphical) ..
Can you please explain to me why?
In the first case, you reset the value of sum to zero on each iteration of the outer loop. In the second case, you don't so sum just keeps accumulating.
and how to scope here works?
Variables declared with var have function scope (or global if declared globally). Your problem isn't to do with scope, it's because of where you assign 0 to sum.
if possible please with pictures (graphical)
Sorry, no pictures. :-(
First Case -
i=0
sum = 0;
j iterates till end of loop
sum = 1+2+3+4+5;
i=1
sum = 0;
j iterates till end of loop
sum = 1+2+2+3+1;
i=2
sum = 0;
j iterates till end of loop
sum = 11+12+23+45+88;
....
Second Case -
i=0
sum = 0;
j iterates till end of loop
sum = 1+2+3+4+5;
i=1
sum = 1+2+3+4+5;
j iterates till end of loop
sum = (1+2+3+4+5)+1+2+2+3+1;
i=2
sum = ((1+2+3+4+5)+1+2+2+3+1);
j iterates till end of loop
sum = ((1+2+3+4+5)+1+2+2+3+1)+11+12+23+45+88;
...
The for statement does not define a scope in javascript, only functions do (as per ES5).
Your problem is because the moment you assign 0 to sum: inside the outer for.
As we know, we can omit the initialization inside the for loop :
var i = 0;
for(; i < 10; i++) {
// do Something with i
}
However, i figure out if i omit the initialization inside the nested loop as well:
var i = 0;
var j = 0;
for(; i < 10; i++) {
for(; j < 10; j++) {
// only do Something with j
// outner loop only run once
}
}
Solution :
var i = 0;
var j;
for(; i < 10; i++) {
j = 0;
for(; j < 10; j++) {
// everything is fine!
}
}
Can anyone explain whats going on? I'm newbie in Javascript.
The loop runs multiple times, but your 'j' is already ten after the first run of i, so the second loop no longer runs during the next values of i.
If you would write something like this.
for(; i < 10; i++) {
for(; j < 10; j++) {
console.log("test")
}
console.log("another test")
}
You would see that both messages get printed 10 times.
The reason that your "j" loop, only runs "ten times once", is because you do not set J back to zero after it has run 10 times. So the statement j < 10is true for every loop if iafter the first run.
You could solve this by setting j = 0 inside the first for loop (if you do not want to put it inside the variable initialisation of the second loop.)
for(; i < 10; i++) {
j = 0;
for(; j < 10; j++) {
console.log("test")
}
console.log("another test")
}
Every time i increments, you need to reset j to 0.
If you rewrite your script so that each variable is initialised within the for loop statement itself, then every time i increments, j will be reset to 0.
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
}
}
Is it possible for a for-loop to repeat a number 3 times? For instance,
for (i=0;i<=5;i++)
creates this: 1,2,3,4,5.
I want to create a loop that does this: 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5
Is that possible?
for (i=1;i<=5;i++)
for(j = 1;j<=3;j++)
print i;
Yes, just wrap your loop in another one:
for (i = 1; i <= 5; i++) {
for (lc = 0; lc < 3; lc++) {
print(i);
}
}
(Your original code says you want 1-5, but you start at 0. My example starts at 1)
You can have two variables in the for loop and increase i only when j is a multiple of 3:
for (i=1, j=0; i <= 5; i = ++j % 3 != 0 ? i : i + 1)
Definitely. You can nest for loops:
for (var i = 1; i < 6; ++i) {
for(var j = 0; j < 3; ++j) {
print(i);
}
}
Note that the code in your question will print 0, 1, 2, 3, 4, 5, not 1, 2, 3, 4, 5. I have fixed that to match your description in my answer.
Just add a second loop nested in the first:
for (i = 0; i <= 5; i++)
for (j = 0; j < 3; j++)
// do something with i
You can use nested for loops
for (var i=0;i<5; i++) {
for (var j=0; j<3; j++) {
// output i here
}
}
You can use two variables in the loop:
for (var i=1, j=0; i<6; j++, i+=j==3?1:0, j%=3) alert(i);
However, it's not so obvious by looking at the code what it does. You might be better off simply nesting a loop inside another:
for (var i=1; i<6; i++) for (var j=0; j<3; j++) alert(i);
I see lots of answers with nested loops (obviously the nicest and most understandable solution), and then some answers with one loop and two variables, although surprisingly nobody proposed a single loop and a single variable. So just for the exercise:
for(var i=0; i<5*3; ++i)
print( Math.floor(i/3)+1 );
You could use a second variable if you really only wanted one loop, like:
for(var i = 0, j = 0; i <= 5; i = Math.floor(++j / 3)) {
// whatever
}
Although, depending on the reason you want this, there's probably a better way.