1

I'm writing a for loop for a project that prompts the user to input a number and keeps prompting, continually adding the numbers up. When a string is introduced, the loop should stop. I've done it with a while loop, but the project states that we must do it with a for loop also. The problem is that the prompt keeps running even when 'a = false'. Could someone explain javascript's thinking process? I want to understand why it keeps running back through the loop even though the condition isn't met. Thank you

 var addSequence2 = function() {

        var total = 0;
        var a;

        for (; a = true; ) {
            var input = prompt("Your current score is " +total+ "\n" + "Next number...");

            if (!isNaN(input)) {
                a = true;
                total = +total + +input;
            }
            else if (isNaN(input)) {
                a = false;
                document.write("Your total is " + total);
            }
        }
 };
CodeNovice
  • 343
  • 1
  • 11

8 Answers8

3

There is a difference between a = true and a == true.

Your for-loop is basically asking "can I set 'a' to true?", to which the answer is yes, and the loop continues.

Change the condition to a == true (thus asking "Is the value of 'a' true?")


To elaborate, in most programming languages, we distinguish between assignment ("Make 'x' be 4") and testing for equality ("Is 'x' 4?"). By convention (at least in languages that derive their syntax from C), we use '=' to assign/set a value, and '==' to test.

If I'm understanding the specification correctly (no guarantee), what happens here is that the condition condenses as follows:

  1. Is (a = true) true?
  2. Complete the bracket: set a to true
  3. Is (a) true? (we just set it to true, so it must be!)
KidneyChris
  • 857
  • 5
  • 12
  • Thank you @KidneyChris, and everyone else who replied. I understand now. I appreciate you all taking the time to help me :) – CodeNovice Nov 27 '14 at 12:06
2

Try using the equal to operator, i.e. change

for (; a = true; ) {

to

for (; a == true; ) {

Resource
  • 524
  • 4
  • 16
1

You should use a == true instead of a = true......= is an assignment operator

santa banta
  • 313
  • 1
  • 2
  • 17
1

for (; a = true; ), you are assigning the value to the variable "a" and it will always remain true and will end up in infinite loop. In JavaScript it should a===true.

JSK
  • 583
  • 6
  • 17
0

I suspect you want your for to look like this :

for(;a==true;)

as a=true is an assignment, not a comparison.

GodEater
  • 3,445
  • 2
  • 27
  • 30
0

a == true. The double equal sign compares the two. Single equal assigns the value true to a so this always returns true.

AllwinP
  • 71
  • 4
0

for (; a = true; ) <-- this is an assignation

for (; a == true; ) <-- this should be better

Gerald
  • 690
  • 4
  • 13
0

Here's your fixed code :

var addSequence2 = function() {

    var total = 0;
    var a = true;

    for(;Boolean(a);) {
        var input = prompt("Your current score is " +total+ "\n" + "Next number...");

        if (!isNaN(input)) {
            total = total + input;
        }
        else{
            a = false;
            document.write("Your total is " + total);
        }
    }
};
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Yes, a while loop seems more appropriate for this task. But the assignment required us to use a for loop to practice using them, and evidently, I needed the practice! Hehe. Thanks for your help :) – CodeNovice Nov 27 '14 at 12:10