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 squaredcube()
must return a copy of the array, containing all values cubedaverage()
must return the average of all array values; on an empty array must returnNaN
(note: the empty array is not tested in Ruby!)sum()
must return the sum of all array valueseven()
must return an array of all even numbersodd()
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]
Arrays
Fundamentals
Object-oriented Programming
Similar Kata:
Stats:
Created | Oct 15, 2013 |
Published | Oct 16, 2013 |
Warriors Trained | 36782 |
Total Skips | 4511 |
Total Code Submissions | 96230 |
Total Times Completed | 23801 |
JavaScript Completions | 21610 |
Ruby Completions | 1967 |
C# Completions | 349 |
Total Stars | 473 |
% of votes with a positive feedback rating | 92% of 1640 |
Total "Very Satisfied" Votes | 1418 |
Total "Somewhat Satisfied" Votes | 185 |
Total "Not Satisfied" Votes | 37 |