1

I want to ask the user a series of questions (this is only for college hence the prompt's and not a proper UI), and to cut down on the LOC I added all the questions to an array called !questions"

For some reason the loop is only including every even number in the array.

The code I am using is below

    var questions = ['How old is Mark Zuckerberg?',
                     'How much is he worth?',
                     'How old is Bill Gates?',
                     'How much is he worth?',
                     'How old is Dennis Ritchie?'];

    for (var i = 0; i < 6; i++)
    {
        prompt(questions[i++]);
    }

If anyone could help me to get it prompting every question from the array I would very much appreciate this.

1
  • 4
    You are incrementing i twice in each loop iteration. Commented Feb 11, 2014 at 14:27

3 Answers 3

5

You're incrementing i twice each time the loop iterates.

Try:

for (var i = 0; i < 6; i++)
{
    prompt(questions[i]);
}

Or:

for (var i = 0; i < 6; )
{
    prompt(questions[i++]);
}

Either will increment i once per loop iteration.

Sign up to request clarification or add additional context in comments.

Comments

4

Use

prompt(questions[i]);

Instead of

prompt(questions[i++]);

Comments

1

your error is that u put i++, u should only put i, increases so does the loop

for (var i = 0; i < 6; i++)
    {
        prompt(questions[i]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.