Marco Maher
Marco Maher

Reputation: 351

trying to make a rock paper scissors game in java script

when I run in it this error appears [Uncaught TypeError: rpsdatabase is not a function]

the output should be something like [0,1] which means human choose rock and bot choose paper I left the error line between two empty to find it easily

function rpsGame(yourChoice){
    console.log(yourChoice);
    var humanChoice, botChoice;
    humanChoice = yourChoice.id;
    botChoice = numbertochoice(randombot());
    console.log('computer choice: ', botChoice)
    results = Winner(humanChoice, botChoice)

    //messege = finaleMessege(results);
    //rpsfrontend(yourChoice.id, botChoice, messege);
}

function randombot() {        
    return Math.floor(Math.random() * 3)
}

function numbertochoice(number){
    return ['rock', 'paper', 'scissors'][number]
}

function Winner(humanChoice, botChoice){
    var rpsdatabase = {        
        'rock': {'scissors': 1, 'paper': 0,'rock': 0.5 },
        'paper': {'rock': 1, 'scissors':0, 'paper': 0.5},
        'scissors': {'paper': 1, 'rock':0, 'scissors': 0.5},
    };

    var yourScore = rpsdatabase(humanChoice)(botChoice);
    var botScore = rpsdatabase(humanChoice)(yourChoice);

    return (yourScore, botScore);

Upvotes: 2

Views: 101

Answers (1)

Yakko Majuri
Yakko Majuri

Reputation: 468

Don't use parentheses to access an object. Do it like this

var yourScore = rpsdatabase[humanChoice][botChoice];
var botScore = rpsdatabase[humanChoice][yourChoice];

Upvotes: 2

Related Questions