Loop to get repeated prompt till any condition is false


I am making RPS Game without UI

here I want to know how can i get prompt repeatedly if there’s a tie

Here’s My code so far:

const array = ["rock", "paper", "scissors"];

//to play theround
function playerRound(player, computer) {

    if(player.toLowerCase() === computer) {
        return repeatPrompt();
    } else if(player.toLowerCase() === array[0] && computer === array[1]) {
        return "You Lose";
    } else if(player.toLowerCase() === array[0] && computer === array[2]) {
     return "You Won";
    } else if(player.toLowerCase() === array[1] && computer === array[0]) {
        return "You won";
    } else if(player.toLowerCase() === array[1] && computer === array[2]) {
        return "You lose";
    } else if(player.toLowerCase() === array[2] && computer === array[0]) {
        return "You lose";
    } else if(player.toLowerCase() === array[2] && computer === array[1]) {
        return "You Won";

// parameters
const randomNumber = random();
const computer = array[0];
const playerR = prompt("Choose any one from the rock, paper, scissors");

// random Number
function random() {
    return Math.floor(Math.random() * 2);

//repeating Prompt when The Player and Computer Choose Same
function repeatPrompt() {

const repeatAgain = "Y";

while(repeatAgain == "Y") {
    return prompt("Choose any From RPS");

// Play the Game
console.log(playerRound(playerR, computer));

// return  prompt repeatedly  if it is not from rock, paper, scissors till it is equal to rock, paper, scissors
// return the prompt repeatedly  if it theres a tie
//Make the Player  play 5 rounds and repeat prompt if theres a tie 
// return the score of 5 PlayRounds
//Declare Who wins and Looses

Hi @arsheencoder78618888 !

You basically want to say if user response and computer are the same then there is a tie.

So, it would be easier to just write something like this.

while (computer === playerR) {
    playerR = prompt('There is a tie. Choose rock, paper, scissors')
//you should also update the computer response so it doesn't return rock everytime

I don’t think you need this stuff here

You just want to compare the computer and player responses.

Also, I would change these to let instead of const because the player and computer responses should change. Especially if you are creating a game that plays 5 rounds.

1 Like

Thanks for replying i will correct it in sha Allah

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.