Skip to content

Instantly share code, notes, and snippets.

@djalmaaraujo
Created August 13, 2018 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djalmaaraujo/eeee6916d37b1f7b6c53386b5f057805 to your computer and use it in GitHub Desktop.
Save djalmaaraujo/eeee6916d37b1f7b6c53386b5f057805 to your computer and use it in GitHub Desktop.
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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment