How can I make this loop/function run correctly?

So I am making a rock paper scissor game that uses prompts to play

this is the entire code

var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;

var winStat = 0;
var lossStat = 0;
var tieStat = 0;

function isValid() {
    for (var i = 0; i < options.length; i++) {
        const found = options[i];
        if (userRes === found) {
            return checkVar = true;
        } else if (userRes !== found) {
            return checkVar = false;
        }
    }
}

function getCompChoice() {
    let compSet = options[Math.floor(Math.random() * options.length)];
    compChoice = compSet.toUpperCase();
    console.log(compChoice)
    return alert('The computer chose ' + compChoice);
}

function getUserChoice () {
    userSet = prompt('Rock Paper or Scissors?');
    if (userSet === null) {
        return startGame();
    } else {
        userRes = userSet.toUpperCase();
    }
    isValid()
    if (checkVar === true) {

        console.log('continue')
        userRes.toUpperCase();
        console.log(userRes);

        getCompChoice()

        if((userRes === 'R' && compChoice === 'S') ||
        (userRes === 'P' && compChoice === 'R' ||
        (userRes === 'S' && compChoice === 'P'))) {
            console.log('win')
            alert('You Won !')
            checkStat = true
            playAgain()
        } else if (userRes === compChoice) {
            console.log('tie')
            alert('You Tied !')
            checkStat = null
            playAgain()
        } else if (userRes !== compChoice) {
            console.log('loss')
            alert('You Lost !')
            checkStat = false
            playAgain()
        }
    } else if (checkVar === false) {
        console.log('end')
        console.log(userRes);

        alert('Please enter R, P, or S. (Not case sensitive).');
        getUserChoice();
    }

}

function playAgain() {
    if (checkStat) {
        winStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (checkStat === null){
        tieStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (!checkStat) {
        lossStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    }

    pAgain = confirm('Play Again ?')
    if (pAgain) {
        getUserChoice();
    }
}

function startGame () {
    askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
    if (askUser) {
        return getUserChoice();
    } else if (!askUser) {
        return alert('Come back next time !')
    }
}

startGame();

now my problem here is that everything works as expected however the function isValid() only loops once and outputs only ‘R’ into my console log which breaks the game entirely and I can only use r to play using ‘S’ or ‘P’ returns the alert saying i did not enter r p or s if anyone can help me understand why the loop is only running once or only outputting ‘R’ that would be great ive been stumped on this for a while

just an fyi I am still fairly new to javascript

I tried using a const to change my array to string outputs in console but that also did not do anything.

>Solution :

The problem is that you are returning early!

It is like saying:

function isValid() {
  return checkVar = (userRes === options[0]);
}

So the solution would be to check for all results before returning:

function isValid() {
    for (var i = 0; i < options.length; i++) {
        const found = options[i];
        if (userRes === found) {
            return checkVar = true;
        }
    }

    checkVar = false;
}
var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;

var winStat = 0;
var lossStat = 0;
var tieStat = 0;

function isValid() {
    for (var i = 0; i < options.length; i++) {
        const found = options[i];
        if (userRes === found) {
            return checkVar = true;
        }
    }
    
    checkVar = false;
}

function getCompChoice() {
    let compSet = options[Math.floor(Math.random() * options.length)];
    compChoice = compSet.toUpperCase();
    console.log(compChoice)
    return alert('The computer chose ' + compChoice);
}

function getUserChoice () {
    userSet = prompt('Rock Paper or Scissors?');
    if (userSet === null) {
        return startGame();
    } else {
        userRes = userSet.toUpperCase();
    }
    isValid()
    if (checkVar === true) {

        console.log('continue')
        userRes.toUpperCase();
        console.log(userRes);

        getCompChoice()

        if((userRes === 'R' && compChoice === 'S') ||
        (userRes === 'P' && compChoice === 'R' ||
        (userRes === 'S' && compChoice === 'P'))) {
            console.log('win')
            alert('You Won !')
            checkStat = true
            playAgain()
        } else if (userRes === compChoice) {
            console.log('tie')
            alert('You Tied !')
            checkStat = null
            playAgain()
        } else if (userRes !== compChoice) {
            console.log('loss')
            alert('You Lost !')
            checkStat = false
            playAgain()
        }
    } else if (checkVar === false) {
        console.log('end')
        console.log(userRes);

        alert('Please enter R, P, or S. (Not case sensitive).');
        getUserChoice();
    }

}

function playAgain() {
    if (checkStat) {
        winStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (checkStat === null){
        tieStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    } else if (!checkStat) {
        lossStat++;
        alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
    }

    pAgain = confirm('Play Again ?')
    if (pAgain) {
        getUserChoice();
    }
}

function startGame () {
    askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
    if (askUser) {
        return getUserChoice();
    } else if (!askUser) {
        return alert('Come back next time !')
    }
}

startGame();

Leave a Reply