5 kyu

Randomize Objects

320 of 322surtich

Description:

Implement the following methods:

Object.prototype.random(): returns randomly one of the values of the object.

For example:

var obj = {
    a: 1,
    b: {
        x: 2,
        y: 3
    },
    c: {
        z: {
            q: 4
        }
    }
};

obj.random(); //returns 1 or 2 or 3 or 4. All values have the same probability to be returned

obj = {};

obj.random(); //returns undefined

Object.prototype.toRandomArray(): returns an array of the random values.

For example,

var obj = {
    a: 1,
    b: {
        x: 2,
        y: 3
    },
    c: {
        z: {
            q: 4
        }
    }
};

obj.toRandomArray(); //returns a random permutation of [1, 2, 3, 4]

obj = {};

obj.toRandomArray(); //returns []
Algorithms
Fundamentals
Arrays

Similar Kata:

Stats:

CreatedFeb 18, 2014
PublishedFeb 18, 2014
Warriors Trained872
Total Skips133
Total Code Submissions4915
Total Times Completed322
JavaScript Completions320
Total Stars33
% of votes with a positive feedback rating90% of 62
Total "Very Satisfied" Votes52
Total "Somewhat Satisfied" Votes7
Total "Not Satisfied" Votes3
Ad
Contributors
  • surtich Avatar
  • jhoffner Avatar
Ad