diff --git a/lib/event.rb b/lib/event.rb new file mode 100644 index 0000000..1fa0f59 --- /dev/null +++ b/lib/event.rb @@ -0,0 +1,75 @@ +class Event + + attr_reader :name, :ages + def initialize(event_name, ages_for_event) + @name = event_name + @ages = ages_for_event + end + + def max_age + max_age_holder = 0 + @ages.each do |age| + + if age > max_age_holder + max_age_holder = age + end + + end + max_age_holder + end + + def min_age + min_age_holder = 99999999 + + @ages.each do |age| + + if age < min_age_holder + min_age_holder = age + end + + end + + min_age_holder + end + + def average_age + average_age_holder = 0.0 + + @ages.each do |age| + average_age_holder += age + end + + value = average_age_holder / @ages.size + value.round(2) + end + + def standard_deviation_age + step1_value = 0.0 + divide_value = @ages.size + @ages.each do |value| + step1_value += value + end + step3_value = 0.0 + step3_value = step1_value / divide_value + + step4_array = [] + @ages.each do |value| + step4_array << (value - step3_value) + end + + step5_array = [] + step4_array.each do |value| + step5_array << (value * value) + end + + step6_sum = 0 + step5_array.each do |value| + step6_sum += value + end + step7_divide = step6_sum / divide_value + + total = Math.sqrt(step7_divide) + total.round(2) + end + +end diff --git a/lib/games_runner.rb b/lib/games_runner.rb new file mode 100644 index 0000000..882f05d --- /dev/null +++ b/lib/games_runner.rb @@ -0,0 +1,30 @@ +require './lib/event' +require './lib/games' + +puts "What year is it?" +year = gets.chomp +game = Games.new(year) +puts "Type in the name of the event and then the ages of the players." +puts "Type quit to see a summary" +input = '' +puts "What is the name of the event? Type quit here to see the summary" +input = gets.chomp +while input != "quit" do + ages_array = [] + puts "Type in the age of one player. Type in quit to create a different event" + age_input = '' + if input != 'quit' + while age_input != 'quit' do + puts "Age of a player: " + age_input = gets.chomp + if age_input != 'quit' + ages_array << age_input.to_i + end # age_input != 'quit' + end # age_input != 'quit' do + end # input != 'quit' + event = Event.new(input, ages_array) + game.add_event(event) + puts "What is the name of the next event? Type quit here to see the summary" + input = gets.chomp +end #while input != "quit" do +puts game.summary diff --git a/lib/standard_deviation.rb b/lib/standard_deviation.rb index e17b03a..2a02aa6 100644 --- a/lib/standard_deviation.rb +++ b/lib/standard_deviation.rb @@ -1,5 +1,33 @@ ages = [24, 30, 18, 20, 41] # Your code here for calculating the standard deviation +def standard_deviation(dev_array) + step1_value = 0.0 + dev_array.each do |value| + step1_value += value + end + step3_value = 0.0 + step3_value = step1_value / dev_array.size + + step4_array = [] + dev_array.each do |value| + step4_array << (value - step3_value) + end + + step5_array = [] + step4_array.each do |value| + step5_array << (value * value) + end + + step6_sum = 0 + step5_array.each do |value| + step6_sum += value + end + step7_divide = step6_sum / dev_array.size + + total = Math.sqrt(step7_divide) + puts total +end # When you find the standard deviation, print it out +standard_deviation(ages) diff --git a/test/event_test.rb b/test/event_test.rb new file mode 100644 index 0000000..e424a37 --- /dev/null +++ b/test/event_test.rb @@ -0,0 +1,41 @@ +require 'minitest/autorun' +require 'minitest/pride' +require './lib/event' + +class EventTest < Minitest::Test + + def test_if_event_exist + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_instance_of Event, event + end + + def test_if_event_has_name + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_equal "Curling", event.name + end + + def test_if_event_has_ages + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_equal [24, 30, 18, 20, 41], event.ages + end + + def test_event_max_age + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_equal 41, event.max_age + end + + def test_event_min_age + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_equal 18, event.min_age + end + + def test_event_average_age + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_equal 26.6, event.average_age + end + + def test_event_standard_deviation + event = Event.new("Curling", [24, 30, 18, 20, 41]) + assert_equal 8.28, event.standard_deviation_age + end +end