+ 5
Logging the number of times something has happened.
I made a simple coin flip script that will output heads or tails. I want the console to log the number of times the coin has been flipped (Flip [#] Result: [result]) Here is my code: https://code.sololearn.com/WpqpQpFvEpvF/#js Thank you for your help :)
1 Answer
+ 5
Defining the flip variable outside the function will make it as you want.
JavaScript Code :
var flips = 0;
function flipCoin(){
// Array for the outcomes possible
var choice = ["Heads", "Tails"];
// Generates random choice of heads or tails
var result = choice[Math.floor(Math.random() * choice.length)];
document.getElementById("result").innerHTML ="Result: " + result;
flips = flips + 1;
// Logs the result of the role
console.log("Flip " + flips + " Result: " + result);
}