Home > Software engineering >  How to take in 2 inputs and give out one output?
How to take in 2 inputs and give out one output?

Time:08-31

This is my code for rock paper scissors. In the if statement, I'm trying to take in 2 inputs to produce one output. How can I do this?

//rock paper scissors
const rock = 1
const paper = 2
const scissors = 3
var player_choice = console.log(Number(prompt("Rock-1, Paper-2, Scissors-3")))
var computer_choice = (Math.random())
var actual_computer_choice = Math.ceil(computer_choice)

if (actual_computer_choice==player_choice) {
  console.log('Draw')
}

if (actual_computer_choice==1) (player_choice==2);
  console.log("Computer chose rock. Human chose paper. Human Wins!")

if (actual_computer_choice==2) and (player_choice==1);
  console.log("Computer chose paper. Human chose rock. Computer Wins!")

if (actual_computer_choice==3) and (player_choice==1);
  console.log("Computer chose scissors. Player chose rock. Player Wins!")

if (actual_computer_choice==1) and (player_choice=3);
  console.lop("Computer chose rock. Player chose scissors. Computer Wins!")

if (actual_computer_choice==2) and (player_choice=3);
  console.log("Computer chose paper. Player chose scissors. Player Wins!")

if (actual_computer_choice==3) and (player_choice=2);
  console.log("Computer chose scissors. Player chose paper. Computer Wins!")

CodePudding user response:

//rock paper scissors
const rock = 1
const paper = 2
const scissors = 3
var player_choice = Number(prompt("Rock-1, Paper-2, Scissors-3"));
console.log(player_choice)
var computer_choice = (Math.random());
var actual_computer_choice = Math.ceil(computer_choice);
console.log(actual_computer_choice);

if (actual_computer_choice == player_choice) {
  console.log('Draw');
}

if (actual_computer_choice==1 && player_choice==2){
  console.log("Computer chose rock. Human chose paper. Human Wins!");
}

if (actual_computer_choice==2 && player_choice==1){
  console.log("Computer chose paper. Human chose rock. Computer Wins!")
}

if (actual_computer_choice==3 && player_choice==1){
  console.log("Computer chose scissors. Player chose rock. Player Wins!");
}

if (actual_computer_choice==1 && player_choice=3){
  console.lop("Computer chose rock. Player chose scissors. Computer Wins!");
}

if (actual_computer_choice==2 && player_choice=3){
  console.log("Computer chose paper. Player chose scissors. Player Wins!");
}

if (actual_computer_choice==3 && player_choice=2){
  console.log("Computer chose scissors. Player chose paper. Computer Wins!");
}

CodePudding user response:

You can use the Logical AND (&&)

if (actual_computer_choice==player_choice) {
  console.log('Draw')
}

if (actual_computer_choice==1 && player_choice==2) {
  console.log("Computer chose rock. Human chose paper. Human Wins!");
}

if (actual_computer_choice==2 && player_choice==1) {
  console.log("Computer chose paper. Human chose rock. Computer Wins!");
}

if (actual_computer_choice==3 && player_choice==1) {
  console.log("Computer chose scissors. Player chose rock. Player Wins!");
}

if (actual_computer_choice==1 && player_choice=3) {
  console.lop("Computer chose rock. Player chose scissors. Computer Wins!");
}

if (actual_computer_choice==2 && player_choice=3) {
  console.log("Computer chose paper. Player chose scissors. Player Wins!");
}

if (actual_computer_choice==3 && player_choice=2) {
  console.log("Computer chose scissors. Player chose paper. Computer Wins!");
}

And i would also recommend using if else statement, so that it does't have to check every possible option.

  • Related