递增和递减变量,直到语句为真

时间:2013-07-04 05:21:58

标签: javascript

所以我有一个值的范围变量,无法确定确切的数字,只有一个接近它的数字(大约50或高于或低于未知数)。所以假设正确的数字是910(再一次,这个数字变化大于或小于已经确定的数字)并且我计算900.我将如何编写尝试900,899,901,898,902的函数, 897 ......依此类推,直到它等于未知变量?

2 个答案:

答案 0 :(得分:1)

var guess = x;
var answer = 910;
var high = guess;
var low = guess;

while (high !== answer && low !== answer) {
    high++;
    low--;
}

if (high === answer) {
    print high;
} else if (low === answer) {
    print low;
} else {
    print "how did you get here?";
}

答案 1 :(得分:0)

由于“未知”号码的测试实际上是服务器调用,因此您无法通过简单的循环执行此操作。相反,你需要异步处理所有事情。这是一个例子:

// Starting with guess, test that value and nearby integers up through
// guess+maxDelta and down through guess-maxDelta.
// Each number is tested by calling testMagicNumber(). This function
// can make an Ajax call or other asynchronous action, and then it
// should call the callback passed to it with true or false.
// When a match is found, call the callback function with that value.
// If no match is found call the callback function with false.
function findMagicNumber( guess, maxDelta, callback ) {
    var delta = 0, sign = +1;
    nextNumber();
    function nextNumber() {
        var value = guess + sign*delta;
        //console.log( 'Testing', value );
        testMagicNumber( value, function( match ) {
            if( match ) {
                callback( value );
            }
            else {
                if( sign > 0)
                    ++delta;
                sign = -sign;
                if( delta <= maxDelta )
                    nextNumber();
                else
                    callback( false );
            }
        });
    }
}

// Test version of a magic number matcher, using a hard coded value
function testMagicNumber( value, callback ) {
    callback( value == magic );
}

// Untested example of a function to test a magic number
// with a server request. isMagic(result) is whatever test
// you need to make on the result to get a boolean value
// for the callback.
function testMagicNumberAjax( value, callback ) {
    $.ajax({
        url: 'test',
        data: { value: value },
        error: function() {
            callback( false );
        },
        success: function( result ) {
            callback( isMagic(result) );
        }
    });
}

for( var magic = 45;  magic <= 55;  ++magic ) {
    console.log( 'Magic number is', magic );
    findMagicNumber( 50, 3, function( result ) {
        console.log( 'Matched', result );
    });
}

这需要猜测50和最大上升和下降3,并测试从45到55的幻数。记录的结果是:

Magic number is 45
Matched false
Magic number is 46
Matched false
Magic number is 47
Matched 47
Magic number is 48
Matched 48
Magic number is 49
Matched 49
Magic number is 50
Matched 50
Magic number is 51
Matched 51
Magic number is 52
Matched 52
Magic number is 53
Matched 53
Magic number is 54
Matched false
Magic number is 55
Matched false

testMagicNumber()函数是您调用服务器进行Ajax调用的地方。当您收到来自通话的回复时,testMagicNumber()应使用callbacktrue调用其false功能参数,以指示该号码是否匹配。在testMagicNumberAjax()上面有一个示例(未经测试的)Ajax版本。

您可以将上面的代码粘贴到Chrome控制台中进行快速测试;更改magic的值以尝试不同的数字。