Why does this recursive function skip numbers?

Gragh

I'm trying to find the various possibilities to equal 100 with digits 1-9. This function produces the desired results, but also others which I had not intended. The other results add up to 100, but without some of these digits, like leaving out 3 or 6. Why are these other results included?

    var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var signs = ["+", "-", "N"];
    var results = [];
    find100("1");
    function find100(expr) {
       if (eval(expr.replace(/N/g, "")) === 100) {
          results.push(expr);
       } else {
          for (var i = eval(expr.substring(expr.length - 1, expr.length)) + 1; i <=    
          nums.length; i++) {
             signs.forEach(function(sign) {
                var expr2 = expr;
                find100(expr2 += sign + i);
             });
          }
       }
    }

Desired output:

1+2+3-4+5+6+78+9, 
1+2+34-5+67-8+9, 
1+23-4+5+6+78-9, 
1+23-4+56+7+8+9, 
12+3+4+5-6-7+89, 
12+3-4+5+67+8+9, 
12-3-4+5-6+7+89, 
123+4-5+67-89, 
123+45-67+8-9, 
123-4-5-6-7+8-9, 
123-45-67+89
Tibrogargan

It's adding undesired results because your first loop iterates through each of the remaining numbers and adds ANY results that evaluate to 100, even if it has skipped a number to do so. If the method finds a solution for a number it adds the solution to results - which is correct, however if it doesn't find a solution it moves onto the next number anyway. This is the source of the skipped numbers. If there was no solution for a number it should have not continued to the next number.

As to how to fix it, that's a different question (but why not ...)

The difference here is that you can ONLY get a result if for any number there exists an expression that uses all remaining numbers.

var results = [];
var operations = [ "+", "-", "" ];
var expected = 100;
var limit = 10;

function findExpression(expr, next) {
    if (next === limit) {
        eval(expr) === expected && results.push(expr);
    } else {
        operations.forEach(function(operation) {
            findExpression(expr + operation + next, next + 1);
        });
    }
}

$(document).ready(function() {
    findExpression("1", 2);
    for(var i=0; i<results.length; i++) {
        $("#foo").append(results[i]+"<br />");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<body>
<div id="foo"></div>
</body>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does a recursive function stop on random numbers?

From Dev

Why does my recursive function always use the same random numbers?

From Dev

Why does the function return None in a recursive count of random numbers?

From Dev

Why does my recursive function to inspect controls skip over every other control?

From Java

Why does my recursive function return None?

From Dev

Why does this recursive function not print done twice?

From Dev

Why does this recursive function return undefined?

From Dev

Why does this recursive python function return None?

From Dev

Why does this recursive function return the wrong value?

From Dev

Why does my recursive function return "undefined"?

From Dev

Why does this recursive javascript function return what it does?

From Dev

Why does When.js promise.then skip a function?

From Dev

Why does my call to an MVS POST function skip the RedirectToAction?

From Dev

Why does the program skip taking input when function is called?

From Dev

Why does the following recursive function give the outputs 'atm' and 'hatm'?

From Dev

Why does this tail recursive function yield <<loop>> error?

From Dev

Why does my recursive function in C cause a stack overflow?

From Dev

Recursive function using lambda's, why does this not work?

From Dev

Why does printf() show unusual behaviour in recursive function?

From Dev

Why does recursive function "infinite" loop return same number

From Dev

Why does not my recursive friend function work in VB6?

From Dev

Why does this recursive function seem to print the reverse of what I expect?

From Dev

Recursive function to print numbers in JavaScript

From Dev

Recursive function to print numbers in JavaScript

From Dev

Why is this recursive function not stopping?

From Dev

Why does my auto-incremented Id skip numbers in SQL Server?

From Dev

MySQL: Why does my INSERT statement skip 56 numbers when auto-incrementing the id?

From Dev

Why does NSNumberFormatter skip a number?

From Dev

Why does it skip the "control" segment?

Related Related

  1. 1

    Why does a recursive function stop on random numbers?

  2. 2

    Why does my recursive function always use the same random numbers?

  3. 3

    Why does the function return None in a recursive count of random numbers?

  4. 4

    Why does my recursive function to inspect controls skip over every other control?

  5. 5

    Why does my recursive function return None?

  6. 6

    Why does this recursive function not print done twice?

  7. 7

    Why does this recursive function return undefined?

  8. 8

    Why does this recursive python function return None?

  9. 9

    Why does this recursive function return the wrong value?

  10. 10

    Why does my recursive function return "undefined"?

  11. 11

    Why does this recursive javascript function return what it does?

  12. 12

    Why does When.js promise.then skip a function?

  13. 13

    Why does my call to an MVS POST function skip the RedirectToAction?

  14. 14

    Why does the program skip taking input when function is called?

  15. 15

    Why does the following recursive function give the outputs 'atm' and 'hatm'?

  16. 16

    Why does this tail recursive function yield <<loop>> error?

  17. 17

    Why does my recursive function in C cause a stack overflow?

  18. 18

    Recursive function using lambda's, why does this not work?

  19. 19

    Why does printf() show unusual behaviour in recursive function?

  20. 20

    Why does recursive function "infinite" loop return same number

  21. 21

    Why does not my recursive friend function work in VB6?

  22. 22

    Why does this recursive function seem to print the reverse of what I expect?

  23. 23

    Recursive function to print numbers in JavaScript

  24. 24

    Recursive function to print numbers in JavaScript

  25. 25

    Why is this recursive function not stopping?

  26. 26

    Why does my auto-incremented Id skip numbers in SQL Server?

  27. 27

    MySQL: Why does my INSERT statement skip 56 numbers when auto-incrementing the id?

  28. 28

    Why does NSNumberFormatter skip a number?

  29. 29

    Why does it skip the "control" segment?

HotTag

Archive