Skip to content

Instantly share code, notes, and snippets.

@djalmaaraujo
Created August 13, 2018 22:53
Show Gist options
  • Select an option

  • Save djalmaaraujo/eeee6916d37b1f7b6c53386b5f057805 to your computer and use it in GitHub Desktop.

Select an option

Save djalmaaraujo/eeee6916d37b1f7b6c53386b5f057805 to your computer and use it in GitHub Desktop.

Revisions

  1. djalmaaraujo created this gist Aug 13, 2018.
    46 changes: 46 additions & 0 deletions muda-que-da.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    const shuffle = (array) => {
    var currentIndex = array.length, temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
    }

    return [... array];
    }

    const letters = ['A', 'B', 'C']
    let correctTimes = 0
    const times = 1000
    let str = ''

    for (let x = 0 ; x < times ; x++) {

    const game = shuffle(letters)
    const correctAnswer = game.indexOf('A')
    const playerChoice = shuffle(game)[0]
    const virouPrimeira = shuffle([...game].filter((i) => ((i !== playerChoice) && i !== 'A')))[0]
    const jogadorTrocouCarta = [...game].find((i) => ((i !== virouPrimeira) && i !== playerChoice))[0]
    const acertou = playerChoice === 'A'

    if (acertou) correctTimes++

    str += `##############\n
    Jogo: ${game.toString()}\n
    Escolha do jogador: ${playerChoice}\n
    Jogo virou: ${virouPrimeira}\n
    Jogador trocou para: ${jogadorTrocouCarta}\n
    Acertou?: ${acertou}\n
    ##############\n`
    }
    document.querySelector('#root').innerText = str
    document.querySelector('#final').innerText = `Porcentagem de acerto ${(correctTimes/times) * 100}%\n`