What am I missing - JavaScript Rock, Paper, Scissors x99

I’m missing alot of test cases for this function. Can anyone see a problem that I’m just missing?

function getRoundWinner(roundNo) {
	if (roundNo >= 1 && roundNo <= 3)
	{
	if (roundNo === 1){
		p1v = playerOneMoveOneValue.
		p1t = playerOneMoveOneType
		p2v = playerTwoMoveOneValue
		p2t = playerTwoMoveOneType
	}
	else if (roundNo === 2){
		p1v = playerOneMoveTwoValue
		p1t = playerOneMoveTwoType
		p2v = playerTwoMoveTwoValue
		p2t = playerTwoMoveTwoType
	}
	else if (roundNo === 3){
		p1v = playerOneMoveThreeValue
		p1t = playerOneMoveThreeType
		p2v = playerTwoMoveThreeValue
		p2t = playerTwoMoveThreeType
	}
	if (p2t === 'scissors' && p1t === 'rock' || p2t === 'rock' && p1t === 'paper'|| p2t === 'paper' && p1t === 'scissors'|| p2t == p1t && p1v > p2v){
		return 'Player One'
	}
	else if (p1t === 'rock' && p2t === 'scissors' || p1t === 'rock' && p2t === 'paper'|| p1t === 'paper' && p2t === 'scissors'|| p1t === p2t && p2v > p1v) {
		return 'Player Two'
	}
	else if (p1t == p2t && p1v == p2v) {
		return 'Tie'
	}
} 
else {
	return null
}
}

Is this a problem with my logic or is it just that my method is controlled for by the test cases?

Here are the ones I triggered for this function:

  3) getRoundWinner() - Main Functionality
       should return the correct winner for round one:
     AssertionError: expected undefined to equal 'Player Two'
      at Object.should.equal (node_modules\chai\lib\chai\interface\should.js:98:35)
      at Context.<anonymous> (test\test.js:237:12)
      at processImmediate (internal/timers.js:461:21)

  4) getRoundWinner() - Main Functionality
       should return the correct winner for round two:
     AssertionError: expected undefined to equal 'Player Two'
      at Object.should.equal (node_modules\chai\lib\chai\interface\should.js:98:35)
      at Context.<anonymous> (test\test.js:246:12)
      at processImmediate (internal/timers.js:461:21)

  5) getRoundWinner() - Main Functionality
       should return the correct winner for round three:
     AssertionError: expected undefined to equal 'Player Two'
      at Object.should.equal (node_modules\chai\lib\chai\interface\should.js:98:35)
      at Context.<anonymous> (test\test.js:255:12)
      at processImmediate (internal/timers.js:461:21)

  6) getRoundWinner() - Edge Cases
       should return null if any move types or values are missing:
     AssertionError: expected undefined to equal null
      at Object.should.equal (node_modules\chai\lib\chai\interface\should.js:98:35)
      at Context.<anonymous> (test\test.js:270:12)
      at processImmediate (internal/timers.js:461:21)

Sorry if it’s something that seems really obvious. I did run the script in node and no errors popped up!

Hi @methodace25279
I think you have to post all of your code including variable declarations and function invocations. Apart from a weird dot at the end of line 5, there aren’t any obvious syntax errors. It’s probably a reference error.
What you could do is to put a console.log() to every if statement and log the variables in order to see if they are correctly defined.

Here is the full code as suggested by mirja_t!

let playerOneMoveOneType 
let playerOneMoveOneValue
let playerOneMoveTwoType
let playerOneMoveTwoValue
let playerOneMoveThreeType
let playerOneMoveThreeValue
let playerTwoMoveOneType
let playerTwoMoveOneValue
let playerTwoMoveTwoType
let playerTwoMoveTwoValue
let playerTwoMoveThreeType
let playerTwoMoveThreeValue

//function setComputerMoves(){
	////1. 3 random moves
//}

function setPlayerMoves(player, moveOneType, moveOneValue, moveTwoType, moveTwoValue, moveThreeType, moveThreeValue){
	if (moveOneValue + moveTwoValue + moveThreeValue == 99){
	if (moveOneType == 'rock' || 'scissors' || 'paper'&& moveTwoType == 'rock' || 'scissors' || 'paper' && moveThreeType == 'rock' || 'scissors' || 'paper') {

		if (player == 'Player One'){
			playerOneMoveOneType = moveOneType
			playerOneMoveOneValue = moveOneValue
			playerOneMoveTwoType = moveTwoType
			playerOneMoveTwoValue = moveTwoValue
			playerOneMoveThreeType = moveThreeType
			playerOneMoveThreeValue = moveThreeValue
		}
			else if (player == 'Player Two'){
			playerTwoMoveOneType = moveOneType
			playerTwoMoveOneValue = moveOneValue
			playerTwoMoveTwoType = moveTwoType
			playerTwoMoveTwoValue = moveTwoValue
			playerTwoMoveThreeType = moveThreeType
			playerTwoMoveThreeValue = moveThreeValue
		}
	}
	}
}

function getRoundWinner(roundNo) {
	if (roundNo >= 1 && roundNo <= 3)
	{
	if (roundNo === 1){
		p1v = playerOneMoveOneValue.
		p1t = playerOneMoveOneType
		p2v = playerTwoMoveOneValue
		p2t = playerTwoMoveOneType
	}
	else if (roundNo === 2){
		p1v = playerOneMoveTwoValue
		p1t = playerOneMoveTwoType
		p2v = playerTwoMoveTwoValue
		p2t = playerTwoMoveTwoType
	}
	else if (roundNo === 3){
		p1v = playerOneMoveThreeValue
		p1t = playerOneMoveThreeType
		p2v = playerTwoMoveThreeValue
		p2t = playerTwoMoveThreeType
	}
	if ((p2t === 'scissors' && p1t === 'rock') || (p2t === 'rock' && p1t === 'paper')|| (p2t === 'paper' && p1t === 'scissors')|| (p2t == p1t && p1v > p2v)){
		return 'Player One'
	}
	else if (p1t === 'rock' && p2t === 'scissors' || p1t === 'rock' && p2t === 'paper'|| p1t === 'paper' && p2t === 'scissors'|| p1t === p2t && p2v > p1v) {
		return 'Player Two'
	}
	else if (p1t == p2t && p1v == p2v) {
		return 'Tie'
	}
} 
else {
	return null
}
}

function getGameWinner(){
	let p1score
	let p2score
	for (let i=1;i<4;i++){
		switch(getRoundWinner(i)){
			case 'Player One':
			p1score += 1
			break
			case 'Player Two':
			p2score += 1
			break
			case 'Tie':
			break 
		}
	}
	if (p1score > p2score){
		return 'Player One'
	}
	else if (p2score > p1score) {
		return 'Player Two'
		}
	else {
		return 'Tie'
	}
	}

Thanks for the suggestion to run console.logs. I felt for some reason that wasn’t possible with this file but of course I can just call the methods within the file and comment them out later. Will try that.

In the meantime if anyone has any further advice based on the above code, I’d really appreciate anything!

I guess you run the function getGameWinner , right?
This is the error I get:

undefined is not an object (evaluating 'playerOneMoveOneValue.
		p1t = playerOneMoveOneType')"

Did you add the console to your functions? Some functions don’t receive the assigned values from other functions, because you aren’t reassigning or passing them correctly. For example the variable playerOneMoveOneType is undefined when you try to reassign your variable plt

Edit: If you remove the odd character at the end of the predecessing line, it always returns ‘Tie’, which probably isn’t what you’re expecting either. Again adding consoles to check what values you receive helps to spot where its going wrong.

The code above is what was asked for. I just defined everything but didn’t call anything. When I actually assigned values to the 12 variables for the console I failed 25 tests (up from 13 at the beginning) so i think this is what I am expected to do.

If i was actually implementing this myself I would:

  1. Call setplayermove twice to initialise the variables
  2. then getgamewinner (which runs get round winner three times)

EDIT: also, thanks for spotting the . I deleted that. Not sure how it got there.

The following code prints the correct results to the console.

I replaced the switch statement with if/else

11 test cases remaining!

I’m going to try to uncouple my functions so that they stand alone. Then I’ll just check the solution.

Thanks for your help!

let playerOneMoveOneType
let playerOneMoveOneValue
let playerOneMoveTwoType
let playerOneMoveTwoValue
let playerOneMoveThreeType
let playerOneMoveThreeValue
let playerTwoMoveOneType
let playerTwoMoveOneValue
let playerTwoMoveTwoType
let playerTwoMoveTwoValue
let playerTwoMoveThreeType
let playerTwoMoveThreeValue

//function setComputerMoves(){
	////1. 3 random moves
//}



function setPlayerMoves(player, moveOneType, moveOneValue, moveTwoType, moveTwoValue, moveThreeType, moveThreeValue){
	if (moveOneValue + moveTwoValue + moveThreeValue == 99){
	if (moveOneType == 'rock' || 'scissors' || 'paper' && moveTwoType == 'rock' || 'scissors' || 'paper' && moveThreeType == 'rock' || 'scissors' || 'paper') {

		if (player == 'Player One'){
			playerOneMoveOneType = moveOneType
			playerOneMoveOneValue = moveOneValue
			playerOneMoveTwoType = moveTwoType
			playerOneMoveTwoValue = moveTwoValue
			playerOneMoveThreeType = moveThreeType
			playerOneMoveThreeValue = moveThreeValue
		}
			else if (player == 'Player Two'){
			playerTwoMoveOneType = moveOneType
			playerTwoMoveOneValue = moveOneValue
			playerTwoMoveTwoType = moveTwoType
			playerTwoMoveTwoValue = moveTwoValue
			playerTwoMoveThreeType = moveThreeType
			playerTwoMoveThreeValue = moveThreeValue
		}
	}
	}
}

//setPlayerMoves('Player One', 'rock', 33, 'rock', 33, 'paper', 33)
//setPlayerMoves('Player Two', 'rock', 34, 'rock', 12, 'rock', 53)

function getRoundWinner(roundNo) {
	if (roundNo >= 1 && roundNo <= 3)
	{
	if (roundNo === 1){
		p1v = playerOneMoveOneValue
		p1t = playerOneMoveOneType
		p2v = playerTwoMoveOneValue
		p2t = playerTwoMoveOneType
	}
	else if (roundNo === 2){
		p1v = playerOneMoveTwoValue
		p1t = playerOneMoveTwoType
		p2v = playerTwoMoveTwoValue
		p2t = playerTwoMoveTwoType
	}
	else if (roundNo === 3){
		p1v = playerOneMoveThreeValue
		p1t = playerOneMoveThreeType
		p2v = playerTwoMoveThreeValue
		p2t = playerTwoMoveThreeType
	}
	if ((p2t === 'scissors' && p1t === 'rock') || (p2t === 'rock' && p1t === 'paper')|| (p2t === 'paper' && p1t === 'scissors')|| (p2t == p1t && p1v > p2v)){
		return 'Player One'
	}
	else if (p1t === 'rock' && p2t === 'scissors' || p1t === 'rock' && p2t === 'paper'|| p1t === 'paper' && p2t === 'scissors'|| p1t === p2t && p2v > p1v) {
		return 'Player Two'
	}
	else if (p1t == p2t && p1v == p2v) {
		return 'Tie'
	}
} 
else {
	return null
}
}

function getGameWinner(){
	let p1score = 0
	let p2score = 0
	for (let i=1;i<4;i++){
		let winner = getRoundWinner(i)
		if (winner === 'Player One'){
			p1score = 1 + p1score;
			} else if (winner === 'Player Two') {
				p2score = 1 + p2score;
			}
			else if(winner === 'Tie'){
				//do nothing
			}
		
		console.log(p1score, p2score, winner);
	}
	if (p1score > p2score){
		return 'Player One'
	}
	else if (p2score > p1score) {
		return 'Player Two'
		}
	else if (p2score === p1score) {
		return 'Tie'
	}
	}
//console.log(getGameWinner())

Got all test cases!

  Player Moves
    √ a global variable called playerOneMoveOneType should exist and initialize to undefined
    √ a global variable called playerOneMoveTwoType should exist and initialize to undefined
    √ a global variable called playerOneMoveThreeType should exist and initialize to undefined
    √ a global variable called playerTwoMoveOneType should exist and initialize to undefined
    √ a global variable called playerTwoMoveTwoType should exist and initialize to undefined
    √ a global variable called playerTwoMoveThreeType should exist and initialize to undefined
    √ a global variable called playerOneMoveOneValue should exist and initialize to undefined
    √ a global variable called playerOneMoveTwoValue should exist and initialize to undefined
    √ a global variable called playerOneMoveThreeValue should exist and initialize to undefined
    √ a global variable called playerTwoMoveOneValue should exist and initialize to undefined
    √ a global variable called playerTwoMoveTwoValue should exist and initialize to undefined
    √ a global variable called playerTwoMoveThreeValue should exist and initialize to undefined

  setPlayerMoves() - Main Functionality
    √ a function called setPlayerMoves should exist
    √ should set player one's moves with valid inputs
    √ should set player two's moves with valid inputs

  setPlayerMoves() - Edge Cases
    √ should not set moves if a move type is missing
    √ should not set moves if an invalid player is supplied
    √ should not set moves if a move value is missing
    √ should not set moves if an invalid move type is supplied
    √ should not set moves if any move values are less than one
    √ should not set moves if any move values are greater than ninety-nine
    √ should not set moves if move values sum to more than ninety-nine

  getRoundWinner() - Main Functionality
    √ a function called getRoundWinner should exist
    √ should return the correct winner with rock versus scissors
    √ should return the correct winner with rock versus paper
    √ should return the correct winner with paper versus scissors
    √ should return the correct winner with two of the same move type and different values
    √ should return the correct winner with two of the same move type and the same value
    √ should return the correct winner for round one
    √ should return the correct winner for round two
    √ should return the correct winner for round three

  getRoundWinner() - Edge Cases
    √ should return null for an invalid round number
    √ should return null if any move types or values are missing

  getGameWinner() - Main Functionality
    √ a function called getGameWinner should exist
    √ should declare when player one wins
    √ should declare when player two wins
    √ should declare when the game is a tie

  getGameWinner() - Main Functionality
    √ should return null if not all values are set

  BONUS: setComputerMoves()
    √ a function called setComputerMoves should exist
    √ should set player two's move types to valid move types
    √ should set player two's move values to valid move values
    √ should set player two's move values to three values that sum to ninety-nine

I found just going to the actual test js file and reading what the actual test cases were doing really helped. For example, the test cases basically initialize certain values in your global namespace before calling each function, rather than passing the data into the function directly.

I thought it would be hard to understand but actual the test cases are almost written in plain English!

Thanks for your help! I learnt alot from this project.

1 Like