Logging the number of times something has happened. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 :)

19th Nov 2018, 10:43 PM
Yosharu
Yosharu - avatar
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); }
20th Nov 2018, 9:49 AM
Raj Chhatrala
Raj Chhatrala - avatar