0

I'm constructing a loop for with a function.

The function loop takes a value, a test function, an update function, and a body function. Each iteration, it first runs the test function on the current loop value and stops if that returns false. Then it calls the body function, giving it the current value. Eventually, it calls the update function to create a new value and starts from the beginning.

loop(10, n => n > 0, n => n - 1, console.log);

function loop(a, b, c, d) {

    let currentValue = a;
    let i;
    for (i = 0; i < currentValue; i++) {
        if (b(currentValue)) {
            d(currentValue);
            update(c);

            function update(c) {
                var executeUpdate = c(currentValue);
                currentValue = executeUpdate;
            };
        } else {
            return;
        }
    };
}


// OUTPUT: 10, 9, 8, 7, 6

Why does this function stop at 6 instead of 1?

2
  • 2
    b returns a bool, and current value is in your example int. how does your if statement work? if (bool < int) Commented Jun 28, 2018 at 13:53
  • @Fallenreaper Right, I just edited the function. Oddly the output doesn't chance. Commented Jun 28, 2018 at 14:00

2 Answers 2

1

function update(c) {
  var executeUpdate = c(currentValue);
  console.log('value of exeucteUpdate: ',executeUpdate, 'when i:', i)
  currentValue = executeUpdate;
};

Do a console.log at your update function, you will notice when i == 4, executeUpdate is 5 and you updated the value of forloop and hence the loop terminates at this particular loop

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

1 Comment

Gotcha. Anchoring the for loop check clause to a static value of a, (i = 0; i < a; i++) would fix this.
1

You can use few console.logs to see it.

Actually the for-cycle ends when currentValue and i equals 5, therefore the condition is not met and cycle terminates.

However your condition does not make any sense, you are comparing true to some number (as you can see in the logs)

loop(10, n => n > 0, n => n - 1, console.log);

function loop(a, b, c, d) {

    let currentValue = a;
    let i;
    for (i = 0; i < currentValue; i++) {
        console.log(i, currentValue);
        console.log(b(currentValue), currentValue)
        if (b(currentValue) < currentValue) {
            d(currentValue);
            update(c);

            function update(c) {
                var executeUpdate = c(currentValue);
                currentValue = executeUpdate;
            };
        } else {
            console.log('I am not here');
            return;
        }
    }
    console.log('finished', i, currentValue);
}

1 Comment

True. I just edited the condition, but oddly the output it's still the same. > Actually the for-cycle ends when currentValue and i equals 5, therefore the condition is not met and cycle terminates. currentValue is set as 10.

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.