Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Project_Mastermind</title>
<script>
    var colors = ['yellow', 'green', 'red', 'blue', 'orange'];
    var arrayLength = colors.length; // WARN: var arrayLengte = colors.length;
   
    function randomArrayItem(array) {
        // ERR: Math.random(kleuren)
        var index = Math.floor(Math.random() * array.length);
        return array[index];
    }
   
    function randomColor() {
        return randomArrayItem(colors);
    }
   
    function randomColorComb(number) {
        var result = [];
       
        // WARN: i < aantal;
        for (var i = 0; i < number; i++) {
                result.push(randomColor());
        }
        return result;
    }
       
    function arrayPlayer() {
            var player = [];
           
            // WARN: i < aantal;
            for (var i = 0; i < arrayLength; i++) {
                    player[i] = prompt("Please fill in a color between ' '", "''");
            }
            return player;
    }
       
    function comparison(computer, player) {
        console.log(computer);
        console.log(player);
        
        var arrayComparison = [];
       
        for(var i = 0; i < arrayLength; i++) {
                // ERR?
                if (computer[i] === player[i]) {
                        arrayComparison.push("1");
                }
                else {
                        arrayComparison.push("0");
                }
        }
        
        console.log(arrayComparison);
        return arrayComparison;
    }
    //
    function correctAnswers(array) {
        //var array = comparison(); ERR
        var correctCounter = 0;
           
        for (var i = 0; i < arrayLength; i++) {
                // ERR
                if (parseInt(array[i]) === 1) {
                        alert("Color number " + i + " is correct");
                        correctCounter += 1;
                }
                else {
                        alert("Color number " + i + " is not correct");
                }
        }
        return correctCounter;
    }
       
    function startGame()
    {
        var code = randomColorComb(arrayLength);
        console.log(code);
        
        //var aantalpogingen = true;
        var gameDone = false;
        var attempCounter = 0;
        var maxNumOfAttempts = 2; // 15
        var comparedArray;
       
        // ERR  || to &&
        while (gameDone !== true && attempCounter < maxNumOfAttempts) {
            var a = arrayPlayer();
            console.log("a: " + a);
            
            console.log(typeof code);
            console.log(typeof a);
            comparedArray = comparison(code, a);
            console.log(comparedArray);
            //var correctAnswerPlayer = correctAnswers();
            var correctAnswerPlayer = correctAnswers(comparedArray);
            console.log("Number of correct answers: " + correctAnswerPlayer);
            if (correctAnswerPlayer !== arrayLength) {
                attempCounter += 1;
                gameDone = false;
            }
            else {
                gameDone = true;
            }
        }
    }
       
    function questionAgain() {
        var x;
        var r = confirm("Press OK if you want to play again.");
       
        // WAR: just return r
        if (r === true) {
                x = true;
        }
        else {
                x = false;
        }
        return x;
    }
       
    function startNewGame()//
    {
        var newGame = true;
       
        while(newGame !== false) {
            startGame();  //Dit werkt nog niet goed. Ik heb geen idee waarom ik geen prompts krijg van --> arraySpeler.
            newGame = questionAgain();
        }
    }
       
    function startMasterMind() {
            startNewGame();
    }
</script>
</head>
<body>
<button onclick="startMasterMind()">start mastermind</button>
  </body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers