-2

ここでの私のゲームは、推測の数を数え、繰り返しの推測を含まない推測ゲームです。

関数試行から関数試行に変数の試行を渡そうとしていますが、機能しません。カウントは 0 のままですが、sameGuess.length を渡すと機能します。これはなぜですか?

let random = Math.round(Math.random()*100);
let guess = false;
let sameGuess = []
let tries = sameGuess.length;


function game(){
    while (guess === false){
        let myGuess = prompt('Guess a number between 0-100:');
        numCheck(myGuess);
        if (myGuess == random){
            guess = true;
            repeats(myGuess, sameGuess);
            attempts(tries, sameGuess);    
        }else if (myGuess < random){
            repeats(myGuess, sameGuess);
            alert('Your number was too small, try again!');
            guess = false;    
        }else if (myGuess > random){
            repeats(myGuess, sameGuess);
            alert('Your answer was too big, try again!');
            guess = false;
        }
    }
}

function attempts(tries, sameGuess){
    if (sameGuess.length == 1){
        alert('Well done, you got it frist try!');
        document.write("<h1>GUESSING GAME</h1><p>Thank you for playing the Guessing Game <br> Created by Jonathan Fox</p>");
    }else if (sameGuess.length <= 15){
        alert('You took ' + sameGuess.length + ' tries');
        alert('Well done, you didn\'t take too many tries!');
        document.write("<h1>GUESSING GAME</h1><p>Thank you for playing the Guessing Game <br> Created by Jonathan Fox</p>");
    }else if (sameGuess.length >=16){
        alert('You took ' + sameGuess.length + ' tries');
        alert('You got it, but lets see less tries next time!'); 
        document.write("<h1>GUESSING GAME</h1><p>Thank you for playing the Guessing Game <br> Created by Jonathan Fox</p>");
    }
}

function repeats(myGuess, sameGuess){
    if ((sameGuess.indexOf(myGuess)) == -1){
        (sameGuess.push(myGuess));
    }else alert('You have already guessed that number! - Dont worry, i haven\'t counted it!');
}

function numCheck(myGuess){
    if (isNaN(myGuess)){
        alert('Enter a number, don\'t try and be sneaky!');
    }
}

game ();
4

2 に答える 2