简体   繁体   English

JavaScript 函数不传递变量

[英]JavaScript Function not passing variable

My game here is a guessing game, which counts the number of guess and does not include any repeated guesses.我这里的游戏是猜谜游戏,计算猜的次数,不包括任何重复的猜测。

I am trying to pass the variable tries from function attempts to function tries but it will not work.我试图将变量尝试从函数尝试传递到函数尝试,但它不起作用。 The count remains 0, but when I pass sameGuess.length it work, why is this?计数仍然为 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 ();

When you access array.length , that value is copied , meaning it won't update even after you add a value to the array:当您访问array.length ,该值被复制,这意味着即使在您向数组添加值后它也不会更新:

var array = [];
var length = array.length;
console.log(length); // 0

array.push('Some value');
console.log(length); // Still 0, since it was copied, it is _not_ a reference to the live value
console.log(array.length); // 1, this is a live reference to the length of the array

As it stands now, your code works fine, although it looks like you can remove the tries aspect of it and use the sameGuess.length directly, as you are now.就目前而言,您的代码运行良好,尽管看起来您可以像现在一样删除它的tries方面并直接使用sameGuess.length

See this post for more discussion on Pass by Reference or Pass by Value .有关通过引用传递或通过值传递的更多讨论,请参阅此帖子。

You should put tries = sameGuess.length;你应该把trys = sameGuess.length; inside of your while!在你的时间里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM