Skip to main content
Arrow functions be arrow functions, not normal functions
Source Link
FreezePhoenix
  • 1.3k
  • 1
  • 12
  • 27

All in all, a basic review of your question shows that you are creating objects numerous times, when they do not need to be. Other things are pointed out in comments.

const choices = ['rock', 'paper', 'scissors'],
      capitalize = word => { return word.charAt(0).toUpperCase() + word.slice(1); },
      // Should these be made into 1 function, that takes another argument, 'condition', which is 'Win' or 'Lose'?
      winStatement = (winner, loser) => { return `You Win! ${capitalize(winner)} beats ${capitalize(loser)}`; }`,
      loseStatement = (winner, loser) => { return `You Lose! ${capitalize(winner)} beats ${capitalize(loser)}`; },
      winningChoice = {'rock': 'paper', 'paper': 'scissors', 'scissors': 'rock'};

function computerPlay() {
    
    return choices[Math.floor(Math.random() * choices.length)];
}

function playRound(playerSelection, computerSelection) {
 
    
    playerSelection = playerSelection.toLowerCase();
    
    // Also, Consecutive if statements that all return need not be else-if.
    if(playerSelection === computerSelection) {
        return "Oh! It's a tie";
    }
    if(playerSelection === winningChoice[computerSelection]) {
        return winStatement(playerSelection, computerSelection);
    }
    return loseStatement(computerSelection, playerSelection);
}

// Should be improved upon? Actually take input?
const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

All in all, a basic review of your question shows that you are creating objects numerous times, when they do not need to be. Other things are pointed out in comments.

const choices = ['rock', 'paper', 'scissors'],
      capitalize = word => { return word.charAt(0).toUpperCase() + word.slice(1); },
      // Should these be made into 1 function, that takes another argument, 'condition', which is 'Win' or 'Lose'?
      winStatement = (winner, loser) => { return `You Win! ${capitalize(winner)} beats ${capitalize(loser)}`; },
      loseStatement = (winner, loser) => { return `You Lose! ${capitalize(winner)} beats ${capitalize(loser)}`; },
      winningChoice = {'rock': 'paper', 'paper': 'scissors', 'scissors': 'rock'};

function computerPlay() {
    
    return choices[Math.floor(Math.random() * choices.length)];
}

function playRound(playerSelection, computerSelection) {
 
    
    playerSelection = playerSelection.toLowerCase();
    
    // Also, Consecutive if statements that all return need not be else-if.
    if(playerSelection === computerSelection) {
        return "Oh! It's a tie";
    }
    if(playerSelection === winningChoice[computerSelection]) {
        return winStatement(playerSelection, computerSelection);
    }
    return loseStatement(computerSelection, playerSelection);
}

// Should be improved upon? Actually take input?
const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

All in all, a basic review of your question shows that you are creating objects numerous times, when they do not need to be. Other things are pointed out in comments.

const choices = ['rock', 'paper', 'scissors'],
      capitalize = word => word.charAt(0).toUpperCase() + word.slice(1),
      // Should these be made into 1 function, that takes another argument, 'condition', which is 'Win' or 'Lose'?
      winStatement = (winner, loser) => `You Win! ${capitalize(winner)} beats ${capitalize(loser)}`,
      loseStatement = (winner, loser) => `You Lose! ${capitalize(winner)} beats ${capitalize(loser)},
      winningChoice = {'rock': 'paper', 'paper': 'scissors', 'scissors': 'rock'};

function computerPlay() {
    return choices[Math.floor(Math.random() * choices.length)];
}

function playRound(playerSelection, computerSelection) {
    playerSelection = playerSelection.toLowerCase();
    
    // Also, Consecutive if statements that all return need not be else-if.
    if(playerSelection === computerSelection) {
        return "Oh! It's a tie";
    }
    if(playerSelection === winningChoice[computerSelection]) {
        return winStatement(playerSelection, computerSelection);
    }
    return loseStatement(computerSelection, playerSelection);
}

// Should be improved upon? Actually take input?
const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
Source Link
FreezePhoenix
  • 1.3k
  • 1
  • 12
  • 27

All in all, a basic review of your question shows that you are creating objects numerous times, when they do not need to be. Other things are pointed out in comments.

const choices = ['rock', 'paper', 'scissors'],
      capitalize = word => { return word.charAt(0).toUpperCase() + word.slice(1); },
      // Should these be made into 1 function, that takes another argument, 'condition', which is 'Win' or 'Lose'?
      winStatement = (winner, loser) => { return `You Win! ${capitalize(winner)} beats ${capitalize(loser)}`; },
      loseStatement = (winner, loser) => { return `You Lose! ${capitalize(winner)} beats ${capitalize(loser)}`; },
      winningChoice = {'rock': 'paper', 'paper': 'scissors', 'scissors': 'rock'};

function computerPlay() {
    
    return choices[Math.floor(Math.random() * choices.length)];
}

function playRound(playerSelection, computerSelection) {
 
    
    playerSelection = playerSelection.toLowerCase();
    
    // Also, Consecutive if statements that all return need not be else-if.
    if(playerSelection === computerSelection) {
        return "Oh! It's a tie";
    }
    if(playerSelection === winningChoice[computerSelection]) {
        return winStatement(playerSelection, computerSelection);
    }
    return loseStatement(computerSelection, playerSelection);
}

// Should be improved upon? Actually take input?
const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));