Lesson 9 Challenge 3

by nathanesa

JavaScript

// Generate a simple array of 60 numbers.
var simpleNumbers = [];
for (var i = 1; i < 61; i ++) {
  simpleNumbers.push(i);
}

document.write("Here's the simple array: " + simpleNumbers + "<br><br>");

// Generate a curved array of 1000 numbers.
var curvedNumbers = [];
for (i = 0; i < 1000; i ++) {
	var firstRoll = Math.floor(Math.random() * 20) + 1;
	var secondRoll = Math.floor(Math.random() * 20) + 1;
	var thirdRoll = Math.floor(Math.random() * 20) + 1;
  curvedNumbers.push(firstRoll + secondRoll + thirdRoll);
}

document.write("Here's the curved array: " + curvedNumbers + "<br><br>");

// Run the winning or losing test on the simple array.
var totalWins = 0;
for (i = 0; i < 60; i ++) {
  var result = ((simpleNumbers[i] - 25) * 3) ** 2;
  if (result > 1000) {
    totalWins = totalWins + 1;
  }
}

document.write("Out of 60 numbers, there were " + totalWins + " winners. ");
document.write("This is a " + totalWins / 60 * 100 + "% chance of winning.<br><br>");

// Run the winning or losing test on the curved array.
totalWins = 0;
for (i = 0; i < 1000; i ++) {
  result = ((curvedNumbers[i] - 25) * 3) ** 2;
  if (result > 1000) {
    totalWins = totalWins + 1;
  }
}

document.write("Out of 1000 numbers, there were " + totalWins + " winners. ");
document.write("This is a " + totalWins / 1000 * 100 + "% chance of winning.<br>");