Moe Tassi
Moe Tassi

Reputation: 15

Rock Paper Scissors javascript function not displaying result

So, I am trying to get this rock paper scissors game to work, but it only displays the "something else" alert even though both picks were equal (its supposed to show "its a tie!").

this error also appears on the console on line 21: NS_ERROR_XPC_SECURITY_MANAGER_VETO:

Heres the code, not all options are complete (paper on paper etc) this is only a test:

<script>

function compPlay (){
  let comChoice= Math.random();
  console.log(comChoice);
  if (comChoice<=0.33){
    comChoice== "rock";
  } else if (comChoice<=0.66){
    comChoice== "scissors";
  } else {
    comChoice== "paper";
  }
  return;
}

function userPlay (){
  prompt("rock, paper or scissors");
  return prompt;
  }

function thegame (compPlay,userPlay){
  if (compPlay=="rock" && userPlay=="rock") {
    alert("its tied");
  } else {
    alert("something else");
  }
}

compPlay();
userPlay();
thegame();

</script>

Upvotes: 0

Views: 247

Answers (4)

Abhishek Jain
Abhishek Jain

Reputation: 888

Your function does not return comChoice. It should be like this.

function compPlay (){
  let comChoice= Math.random();
  console.log(comChoice);
  if (comChoice<=0.33){
    comChoice== "rock";
  } else if (comChoice<=0.66){
    comChoice== "scissors";
  } else {
    comChoice== "paper";
  }
  return comChoice;
}

function userPlay (){
  var promData = prompt("rock, paper or scissors");
  return promData;
}

function thegame (compPlay,userPlay){
    if (compPlay=="rock" && userPlay=="rock") {
        alert("its tied");
    } else {
        alert("something else");
    }
}

var cChoice = compPlay();
var uChoice = userPlay();
thegame(cChoice,uChoice);

And then you should check using comChoice whether it matches or not.

Upvotes: 1

Euripides Georgantzos
Euripides Georgantzos

Reputation: 636

As already stated there are quite a few errors in the code presented.

first: the assignment of values in function compPlay: You only need one '='

second: the function comPlay doesnt return the requested value comChoice

third: In the end you need only call the 3rd function with the above two functions called as parameters. Check the code below enhanced with the appropriate logs and see if that helps

function compPlay (){

let comChoice= Math.random();
  console.log(comChoice);
  if (comChoice<=0.33){
    comChoice= "rock";
  } else if (comChoice<=0.66){
    comChoice= "scissors";
  } else {
    comChoice= "paper";
  }
  return comChoice;
  }

  function userPlay (){
  var input = prompt("rock, paper or scissors");
  console.log(input);
  return input;
  }

  function thegame (compPlay,userPlay){
  if (compPlay==userPlay) {
    alert("its tied");
  } else {
    alert("something else");
  }
  console.log('compPlay: '+compPlay);
  console.log('userPlay: '+userPlay);
  }

  // compPlay();
  // userPlay();
  thegame(compPlay(),userPlay());

Upvotes: 0

xianshenglu
xianshenglu

Reputation: 5329

there is a lot of errors in your code, try to run this at your console,test worked;

function compPlay() {
    let comChoice = Math.random();
    console.log(comChoice);
    if (comChoice <= 0.33) {
        comChoice = "rock";
    } else if (comChoice <= 0.66) {
        comChoice = "scissors";
    } else {
        comChoice = "paper";
    }
    return comChoice;
}


function userPlay() {
    let promData = prompt("rock, paper or scissors");
    thegame(compPlay(), promData);
}

function thegame(comPlayData, promData) {
    if (comPlayData === promData) {
        console.log(comPlayData, promData);
        alert("its tied");
    } else {
        alert("you:" + promData + ',me:' + comPlayData);
    }
}

userPlay();

Upvotes: 1

vijayst
vijayst

Reputation: 21846

You are not returning comChoice from compPlay. If you return comChoice, it should work!

return; should be replaced by return comChoice;

Upvotes: 1

Related Questions