Constructor functions, arrays and objects

52 Views Asked by At

I am trying to sole these two problems and any input would be appreciated.

My solutions to this one works but the output object keep repeating the "Sample" before the object on an array.

(1) Latin Hypercube Sampling Write a function that produces N pseudo-random samples from a real space D. The function should take in as arguments: - the number of samples to produce (N) - the bounds of each dimension (dmin, dmax) The function should return an array of objects representing the whole set of random number tuples. For example: result = [ { "d1":1, "d2":3 }, { "d1":2, "d2":1 }, { "d1":3, "d2":2 } ];

// Create the function.
const hypercube = (N, dmin, dmax) => {
    const samp = [];
    for(let i = 0; i <= N; i ++){
        const   min = Math.ceil(dmin),
                max = Math.floor(dmax);

        // let build = new latin(min, max);
        // samp.push(build);
        samp.push(new Sample(min, max));
    }
    console.log(samp);
};
// Run the function.
hypercube(2, 1, 6);

// This is the constructor function.
function Sample (min, max) {
    this.d1 = Math.floor(Math.random() * (max - min + 1) + min);
    this.d2 = Math.floor(Math.random() * (max - min + 1) + min);
}

This is the second part, my solution is similar to the first one. I created an array of letters and selected one randomly from that array.

(2) Combinatorics Extend the function to allow for combinatorial data types, i.e., values from a fixed, unordered set. The combinatorial data types should be represented a string. The function should take in as arguments: - the number of samples to produce (N) - the configuration of each dimension: - - for a real number this should be bounds (dmin, dmax) - - for a combinatorial value this should be an array of possible values ( [ ... ] ) For example, the function might produce results for three dimensions: the first two being real numbers in the range [1 - 3] and the third being a combinatorial set ["A", "B", "C"]. As before: the function should return an array of objects. For example:

// Create the function.
const hypercube = (N, dmin, dmax) => {
    const samp = [];
    for(let i = 0; i <= N; i ++){
        const   min = Math.ceil(dmin),
                max = Math.floor(dmax);
                
        // let build = new latin(min, max);
        // samp.push(build);
        samp.push(new Sample(min, max));
    }
    console.log(samp);
};
// Run the function.
hypercube(2, 1, 6);
 
// This is the constructor function.
function Sample (min, max) {
    this.d1 = Math.floor(Math.random() * (max - min + 1) + min);
    this.d2 = Math.floor(Math.random() * (max - min + 1) + min);
}

result = [ { "d1":1, "d2":3, "d3":"B" }, { "d1":2, "d2":1, "d3":"A" }, { "d1":3, "d2":2, "d3":"C" } ];

I would appreciate any input.

Thank you.

0

There are 0 best solutions below