6 kyu

Array Helpers

21,610 of 23,801BattleRattle

Description:

This kata is designed to test your ability to extend the functionality of built-in classes. In this case, we want you to extend the built-in Array class with the following methods: square(), cube(), average(), sum(), even() and odd().

Explanation:

  • square() must return a copy of the array, containing all values squared
  • cube() must return a copy of the array, containing all values cubed
  • average() must return the average of all array values; on an empty array must return NaN (note: the empty array is not tested in Ruby!)
  • sum() must return the sum of all array values
  • even() must return an array of all even numbers
  • odd() must return an array of all odd numbers

Note: the original array must not be changed in any case!

Example

var numbers = [1, 2, 3, 4, 5];

numbers.square();  // must return [1, 4, 9, 16, 25]
numbers.cube();    // must return [1, 8, 27, 64, 125]
numbers.average(); // must return 3
numbers.sum();     // must return 15
numbers.even();    // must return [2, 4]
numbers.odd();     // must return [1, 3, 5]
numbers = [1, 2, 3, 4, 5]

numbers.square()  # must return [1, 4, 9, 16, 25]
numbers.cube()    # must return [1, 8, 27, 64, 125]
numbers.average() # must return 3
numbers.sum()     # must return 15
numbers.even()    # must return [2, 4]
numbers.odd()     # must return [1, 3, 5]
var numbers = new int[] { 1, 2, 3, 4, 5 };
numbers.Square(); // must return [1, 4, 9, 16, 25]
numbers.Cube(); // must return [1, 8, 27, 64, 125]
numbers.Average(); // must return 3
numbers.Sum(); // must return 15
numbers.Even(); // must return [2, 4]
numbers.Odd(); // must return [1, 3, 5]
Arrays
Fundamentals
Object-oriented Programming

Stats:

CreatedOct 15, 2013
PublishedOct 16, 2013
Warriors Trained36782
Total Skips4511
Total Code Submissions96230
Total Times Completed23801
JavaScript Completions21610
Ruby Completions1967
C# Completions349
Total Stars473
% of votes with a positive feedback rating92% of 1640
Total "Very Satisfied" Votes1418
Total "Somewhat Satisfied" Votes185
Total "Not Satisfied" Votes37
Ad
Contributors
  • BattleRattle Avatar
  • jhoffner Avatar
  • leviathan Avatar
  • SPARTAN563 Avatar
  • anter69 Avatar
  • KataSideKick Avatar
  • hobovsky Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • farhanaditya Avatar
Ad