Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var NUM_FLIPS = 100;
- var NUM_NUM_FLIPS = 30;
- var heads = 0;
- var tails = 0;
- flipCoins();
- // This function should flip a coin NUM_FLIPS
- // times, and add the result to an array. We
- // return the result to the caller.
- function flipCoins(){
- var flips = [];
- var topstreak = 0;
- var curstreak = 0;
- for(var i = 0; i < NUM_FLIPS; i++){
- if(Randomizer.nextBoolean()){
- flips.push("Heads");
- heads++;
- curstreak++;
- }else{
- flips.push("Tails");
- tails++;
- if(curstreak > topstreak){
- topstreak = curstreak;
- }
- curstreak = 0;
- }
- }
- printArray(flips);
- longestHeadsStreak(topstreak);
- return topstreak;
- }
- function printArray(arr){
- for(var i = 0; i < arr.length; i++){
- println(i+1 + ": " + arr[i]);
- }
- }
- function longestHeadsStreak(topstreak){
- print("Longest Streak of Heads: " + topstreak);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement