-2

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?

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 ();
  • I see I got a downvote already on the question. Sorry if this is a super obvious question, but we all have to learn and I am new to this world as a whole and want to progress, thanks in advance! – Jonathan Fox Jan 14 '19 at 22:03
  • You never change the value of `tries` anywhere, it will always be `0`. – Barmar Jan 14 '19 at 22:10
  • Because the variable tries holds the value of sameguess.length at the point you assign it (so it’s zero). It does not automatically update, like sameguess.length, when you modify the sameguess array. – James Jan 14 '19 at 22:11
  • Are you under the impression that the assignment `tries = sameguess.length` means that whenever you use `tries` it will recalculate using the current length of `sameguess`? – Barmar Jan 14 '19 at 22:11
  • Yes I was Barmar - but all of the comments have taught me something now! so thank you very much all! – Jonathan Fox Jan 14 '19 at 22:34

2 Answers2

0

When you access array.length, that value is copied, meaning it won't update even after you add a value to the array:

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.

See this post for more discussion on Pass by Reference or Pass by Value.

romellem
  • 5,792
  • 1
  • 32
  • 64
0

You should put tries = sameGuess.length; inside of your while!

Ezequiel Fernandez
  • 954
  • 11
  • 18