颜色猜测字符串的比较

时间:2017-01-09 23:21:54

标签: javascript

我正在处理这段代码我的代码是正确的一半但我在check _guess函数中只有一些问题。我尝试了不同的技术,但我找不到它。因为我是初学者而且我不了解JavaScript。

<html>
<head>
    <script >
    var colors =['blue','cyan','gold','gey','green','magenta','orange','red','white','yellow'];
    var target;
    var count = 0;
    var finished = false;
    var guess_input;

    function do_game(){
        target = Math.floor(Math.random() * colors.length);
        while (check_guess()) {
            guess_input_text = prompt("i am thinking of color + colors" +"which color i am random");
            guess_input = parseInt(guess_input_text);
            count += 1;
            finished = check_guess(guess_input);
        }
    }

    function check_guess( guess_input){
        if (colors.indexOf(guess_input) === 0) {
            alert("Sorry.  I don't recognize your color. \n\n Please try again");
            return false;
        }
        if (guess_input < target) {
            alert("Sorry, your guess is not correct. \n\n Hint: your color is alphabetically lower than mine");
            return false;
        }
        if (guess_input > target) {
            alert("Sorry, your guess is not correct. \n\n Hint: your color is alphabetically higher than mine");
            return false;
        }
        else{
            elem_body = document.getElementsByTagName("body")[0];
            elem_body.style.background = colors[target];
            alert("You are right! You took " + count + " guesses!");
            return false;
        }
        return true;
    }
    </script>
</head>

<body onload="do_game()">
</body>
</html>

0 个答案:

没有答案