Click here to Skip to main content
15,301,414 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
New to programming, copied the program as it is through a youtube tutorial, but still getting an error and unable to find the solution.

ERROR = 0: Unable to get property 'PAPER' of undefined or null reference
java.js (52,4)

Program:

JavaScript
function LOL(yourchoice){
console.log(yourchoice.id);

 var humanchoice, botchoice;

 humanchoice = yourchoice.id;

botchoice = num(random());

console.log("Computer Choice:", botchoice);

result = decidewinner(humanchoice, botchoice) ;
console.log(result);

message = finalmessage(result);
console.log(nessage);
//frontend = (yourchoice.id, bot, message);
}

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

function num(number){
    return ["ROCK", "PAPER", "SCISSORS"][number];
}

function decidewinner(yourchoice, computerchoice){
    var database = {
        "ROCK": {"SCISSORS":1, "ROCK":0.5, "PAPER":0 },
        "PAPER": {"SCISSORS":0, "ROCK":1, "PAPER":0.5 },
        "SCISSORS": {"SCISSORS":0.5, "ROCK":0, "PAPER":1 },
    };
LINE 52:
    var yourscore = database[yourchoice][computerchoice];
    var computerscore = database[computerchoice][yourchoice];

    return [yourchoice, computerscore];

}

function finalmessage([yourscore, computerscore]){
    if (yourscore=== 0){
        return ("Message:", "You LOST!", 'color:','red');
    };
    if (yourscore===1){
        return("Message:", "Hurray! You WON", "color:", "blue");
    }    else{
        return("Message:", "TIED!", "color:", "yellow");
    }
   
}


What I have tried:

Tried to check line 52 of javascript code but there is no code there.
Also, tried the whole program again 3 times, still facing same issue.
Posted
Updated 25-Oct-21 3:52am
v3
Comments
gggustafson 24-Oct-21 15:49pm
   
Three warnings
4 Missing semicolon.
41 'destructuring binding' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
44 Unnecessary semicolon.
Three undefined variables
11 result
12 result
14 result
14 message
15 nessage
Two unused variables
34 yourscore
1 LOL
Richard MacCutchan 25-Oct-21 6:18am
   
I have tried to recreate the problem using your code but it does not fail. You need to provide more information.

1 solution

So go back to the youtube tutorial and ask there. Either you copied it wrong, used it wrongly, or he wrote it wrong - and the latter would be my guess as most "Youtube tutorials" are garbage produced to monetize likes and subscribes by people who have no idea what they are doing ...

If he can't - or won't - answer, then dump it and move on.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900