Javascript nested

for (var k = 5; k < 1; k--) {
 for (var i = 0; i < 10; i+=) {
 
 console.log("k= " + k + ", i= " + i);
 }

}

Not sure what is wrong with my code. I’m trying to create 2 for nested loops that decrements k by 1 and increment i by 2 to produce the following:

k = 5, i = 0

k = 5, i = 2

k = 5, i = 4

k = 5, i = 6

k = 5, i = 8

k = 5, i = 10

k = 4, i = 0

k = 4, i = 2

k = 4, i = 4

k = 4, i = 6

k = 4, i = 8

k = 4, i = 10

k = 3, i = 0

k = 3, i = 2

k = 3, i = 4

k = 3, i = 6

k = 3, i = 8

k = 3, i = 10

k = 2, i = 0

k = 2, i = 2

k = 2, i = 4

k = 2, i = 6

k = 2, i = 8

k = 2, i = 10

k = 1, i = 0

k = 1, i = 2

k = 1, i = 4

k = 1, i = 6

k = 1, i = 8

k = 1, i = 10

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi and welcome to the forum.

This statement is incomplete

Here, i won’t ever reach 10

Also the condition is false, this loop won’t even run:

k = 5
k < 1 // false
1 Like

Thank you. I don’t want the answer, but can someone help me get started and explain what each area the statement means i.e.
for(var k=1; k<10;k++).

have you already done the corresponding challenge on the curriculum, or read the documentation on it?

is there something you do not understand there?

It seems like you do not have any experience with for loops. I would try doing these freeCodeCamp lessons:





1 Like

I have done the corresponding challenge. I just have issues with nested for loops.

There is an example of both incrementing by two and decrementing a loop iterator in those challenges. What exactly can we clarify?

Thank You Jeremy. I will study today what you guys have given me. I appreciate all the help!

The thing I don’t understand is how the first loop stays the same number until the second loop reaches its max. i.e.
k=5, i=0
k=5, i=2
k=5, i=4
etc

Well, the full body of a loop is executed on each iteration.

for (let k = 0; k < 42; k++) {
  // All of this stuff in the loop body executes before k is incremented
}

In your case, you have another loop in the loop body, so the full inner loop needs to execute before the outer loop can increment.

I figured it out. Thanks again for the help!

for (let k=5;k>=1;k--) {

for (let i=0;i<=10;i+=2) {
  
console.log("k= " + k, "i= " + i);
}
     }       
2 Likes

I’m trying to run a while loop till -1 is entered and breaks. Each entered number should be shown in the console. the -1 shouldn’t be added to the sum. I believe I’m on the right track. Thank You.

var s = 0;
var t = 0;
var p = 0;
  while (p<0);
   {
var number = prompt("Enter a positive number to be added to the total or -1 to end. ");
       t += parseInt(number);
       t++;
      console.log("Enter a positive number to be added to the total or -1 to end. " + number);
   }
{
   console.log("The sum of all numbers entered is " + t);

}

first remove thw semicolon

second, p starts at 0, so the consition is false and the loop never run

Hi @Lnxfrk42!

Also, in the future if you have a new question about a javascript challenge it would be better to create a new topic.

It gets confusing if there are multiple discussions on different problems happening in the same thread.