0

I am learning JavaScript through The Odin Project and currently stucked with the Rock-Paper-Scissors exercise. Program below works properly at a single round but when I add a for loop to the game() function to call playRound() for five times and keep score it gives an infinite loop error for the line where for loop is.

The program could be configured differently but this is what the project requests. Any help is appreciated.

  
//random choice generator
  
  function computerPlay() {

    function getRandomArbitrary(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    }

    let number = getRandomArbitrary(0, 3)
    if (number == 0) {
      return 'ROCK'
    } else if (number == 1) {
      return 'PAPER'
    } else {
      return 'SCISSORS'
    }
  }


  /////////////////////////////////////////////////////////////////////////////////////////////

// Single Round Main

  function playRound(playerSelection, computerSelection) {


    playerSelection = playerSelection.toUpperCase()


    if (computerSelection == playerSelection) {
      console.log('DEUCE')
      return 0
    } else if ((playerSelection == 'ROCK' && computerSelection == 'SCISSORS') || (playerSelection == 'PAPER' &&
        computerSelection == 'ROCK') || (playerSelection == 'SCISSORS' && computerSelection == 'PAPER')) {
      console.log('You Win! ' + playerSelection + ' beats ' + computerSelection + '.')
      return 1
    } else if ((playerSelection == 'SCISSORS' && computerSelection == 'ROCK') || (playerSelection == 'ROCK' &&
        computerSelection == 'PAPER') || (playerSelection == 'PAPER' && computerSelection == 'SCISSORS')) {
      console.log('You Lose! ' + computerSelection + ' beats ' + playerSelection + '.')
      return 0
    }
  }

  /////////////////////////////////////////////////////////////////////////////////////////////

  
  function game() {

    let score = 0

    for (i = 0; i < 5; i++) {
      let computerSelection = computerPlay()
      let playerSelection = prompt('Choose one! Rock, Paper or Scissors.')
      let result = playRound(playerSelection, computerSelection)


      if (result == 1) {
        score += 1
      } else {
        score += 0
      }
    }
    console.log(score)
  }


  game()
Titus
  • 22,031
  • 1
  • 23
  • 33
  • 1
    You should be defining the variables in your for loop - `for (let i = 0; ....` – Shiny Dec 29 '19 at 19:50
  • Can you add the code for how you're playing 5 games? – Shiny Dec 29 '19 at 19:56
  • It's in the game() function below the code area. The for loop. – armagansenol Dec 29 '19 at 20:00
  • Oh, I was assuming there would be an extra one - in that case I can't replicate it – Shiny Dec 29 '19 at 20:01
  • This *works*. Weirdly, it's being detected as a potential infinite loop. Weird, not sure why this is the case. Seems to be related to the alert functionality. – VLAZ Dec 29 '19 at 20:03
  • @VLAZ yeah, it works for one round and then gives an error. – armagansenol Dec 29 '19 at 20:08
  • 1
    Runs fine for me (in chrome version 79.0.3945.88) – Simon Crane Dec 29 '19 at 20:17
  • Well, i just now tried the "run code snippet" feature in this page and it worked, then tried it at "repl.it" and it worked properly at there too. I had been using "playcode.io" to test and it was giving the error. Maybe there is something wrong with the "playcode.io". – armagansenol Dec 29 '19 at 20:29
  • 1
    [I posted a question about this](https://stackoverflow.com/questions/59523031/could-using-native-browser-modal-dialogs-in-a-loop-lead-to-potentially-infinite). It appears that for some reason having a modal dialog is being detected as "potentially infinite loop". Happens on JSBin but I guess the platform you're using either employs the same algorithm for analysing the code or might actually be using JSBin for the execution. At any rate, I don't know *why* this is being detected as "potentially infinite". It's either a mistake or there is an actual problem. – VLAZ Dec 29 '19 at 20:38
  • @VLAZ Oh ok, I understand. Thanks for help. – armagansenol Dec 29 '19 at 20:49
  • 1
    So, turns out the analysis is much simpler than I thought. The loop just takes "too much time". On JSBin the loop will get terminated if it takes more than 100ms. And since `prompt` will wait for user input and it takes longer than the timeout, it gets wrongly detected as "potentially infinite". – VLAZ Dec 29 '19 at 20:57
  • It seems sensible. – armagansenol Dec 29 '19 at 21:13

1 Answers1

0

Well, I tried the "run code snippet" feature in this page just now and it worked properly, then tried it at "repl.it" and it worked properly at there too. I had been using "playcode.io" to test the code and it was giving the error. Maybe there is something wrong with the "playcode.io".

So, i think it works.