Advertisement
Guest User

Coin Flip Fun: Longest streak of heads

a guest
Dec 3rd, 2019
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. var NUM_FLIPS = 100;
  2. var NUM_NUM_FLIPS = 30;
  3. var heads = 0;
  4. var tails = 0;
  5. flipCoins();
  6. // This function should flip a coin NUM_FLIPS
  7. // times, and add the result to an array. We
  8. // return the result to the caller.
  9. function flipCoins(){
  10. var flips = [];
  11. var topstreak = 0;
  12. var curstreak = 0;
  13. for(var i = 0; i < NUM_FLIPS; i++){
  14. if(Randomizer.nextBoolean()){
  15. flips.push("Heads");
  16. heads++;
  17. curstreak++;
  18. }else{
  19. flips.push("Tails");
  20. tails++;
  21. if(curstreak > topstreak){
  22. topstreak = curstreak;
  23. }
  24. curstreak = 0;
  25. }
  26. }
  27. printArray(flips);
  28. longestHeadsStreak(topstreak);
  29.  
  30. return topstreak;
  31. }
  32.  
  33. function printArray(arr){
  34. for(var i = 0; i < arr.length; i++){
  35. println(i+1 + ": " + arr[i]);
  36. }
  37. }
  38. function longestHeadsStreak(topstreak){
  39. print("Longest Streak of Heads: " + topstreak);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement