Help me with Code!

function playerMove(name) {
    var computerM = computerMove();
    params.eachRound++;
    if ((name === 'rock' && computerM === 'paper') ||
        (name === 'paper' && computerM === 'scissors') ||
        (name === 'scissors' && computerM === 'rock')) {
        params.computerScore++;
        params.score.innerHTML = params.humanScore + ' - ' + params.computerScore;
        params.output.innerHTML = 'COMPUTER WON. You played ' + name + ', computer played ' + computerM;
        params.progress.push({
            'round-nb': params.eachRound,
            'computer-move': computerM,
            'player-move': name,
            'result': 'Computer Won'
        })
        endRound();
    } else if ((name === 'rock' && computerM === 'scissors') ||
        (name === 'paper' && computerM === 'rock') ||
        (name === 'scissors' && computerM === 'paper')) {
        params.humanScore++;
        params.score.innerHTML = params.humanScore + ' - ' + params.computerScore;
        params.output.innerHTML = 'YOU WON. You played ' + name + ', computer played ' + computerM;
        params.progress.push({
            'round-nb': params.eachRound,
            'computer-move': computerM,
            'player-move': name,
            'result': 'You Won'
        })
        endRound();
    } else {
        params.output.innerHTML = 'DRAW. You played ' + name + ', computer played ' + computerM;
        params.progress.push({
            'round-nb': params.eachRound,
            'computer-move': computerM,
            'player-move': name,
            'result': 'Draw'
        })
    }
}

How can I create new function for params.progress ?

Please put your code in backticks for formatting and include spaces. This is a mess to read.

Just re. what @Ronnehag said, I’ve edited your post but just for future reference:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Not sure what you’re looking for here. params.progress is an array. Are you asking for functions to interact with it?

I think yes, becouse in this code I have a two mistake like DRY - three times use this same place, and single responsibility - playerMove() don’t have to follow this logic of progress.

Well, if DRY is the issue, simply move the common logic OUT of the if statement. What I mean is, the bits that are common can be prepped outside of the if block, and the rendering will happen exactly the same anyway. The only bit that has to change is the score, and the result being pushed, right? So create some sort of thisRoundResult object before the if block initialized with all the juicy bits, and then simply use the if to set the score and the thisRoundObj.result depending on the case.

Here’s a quick mock of how that might work:

function playerMove(name) {
    var computerM = computerMove();
    params.eachRound++;
    
     let thisRoundObj = {
       'round-nb': params.eachRound,
       'computer-move': computerM,
       'player-move': name,
     };
     let result = whoWon({human: name, computer: computerM})
  
    if (result === 'computer') {
        params.computerScore++;
        thisRoundObj.result = 'Computer Won';
    } else if (result === 'human') {
        params.humanScore++;
        thisRoundObj.result = 'You Won';
    } else {
        thisResultObj.result = 'Draw';
    }
  
  // In every case, we display the exact same thing:
  params.score.innerHTML = params.humanScore + ' - ' + params.computerScore;
  params.output.innerHTML = thisRoundObj.result+'. You played ' + name + ', computer played ' + computerM;
  
  // In any event, we push this round onto the progress array.
  params.progress.push(thisRoundObj);
  
  // And, if this wasn't a draw, we can end the round!
  if(thisRoundObj.result !== 'Draw'){
    endRound();}
}

function whoWon({human, computer}){
  switch (human){
    case "rock":
      switch (computer){
        case "paper":
          return "computer";
        case "scissors":
          return "human";
        default: 
          return "draw"
      }
    case "paper":
      switch (computer){
        case "scissors":
          return "computer";
        case "rock":
          return "human";
        default: 
          return "draw"
      }
     case "scissors":
      switch (computer){
        case "rock":
          return "computer";
        case "paper":
          return "human";
        default: 
          return "draw"
      }
   }
}

I also moved the deciding who won logic out of the function into its own, as it was pretty ugly. By doing it this way, I can see at a glance what the function is doing:

  • Make the computer move,
  • Generate a Result object,
  • Find out who actually won (by calling whoWon() ),
  • Depending on who won, modify that Result object,
  • Do some output stuff, regardless of who won.

Regardless of how you handle the deciding who won bit, it’s some ugly logic. Not ugly in how it happens, just structurally verbose.

1 Like