Atlassian - Array Conversion

Question 7 You have an array of arbitrary values. Write a transform function in the global scope that will transform the array to an array of functions that return the original values, so instead of calling a[3], we will call a[3](). For example: var a = ["a", 24, { foo: "bar" }]; var b = transform(a); a[1]; // 24 b[1](); // 24

JavaScript

function transform(array) {
    var result = [];

    /**
     * Assumes that b[i]() is bond to a[i], e.g.:
     *   a[1]; // 24
     *   b[1](); // 24
     *   a[1] = "Hello"; // "Hello" 
     *   b[1](); // "Hello" 
     */
    function buildAccessor(i) {
        return function () {
            return array[i];
        };
    }

    /**
     * Assumes that b[i]() is independent of a[i], e.g.:
     *   a[1]; // 24
     *   b[1](); // 24
     *   a[1] = "Hello"; // "Hello" 
     *   b[1](); // 24 
     */
    function buildUnboundAccessor(i) {
        var elem = array[i];
        return function () {
            return elem;
        };
    }

    for (var i = 0; i < array.length; i++) {
        // To stop data binding uncomment this line and comment bellow
        // result.push(buildUnboundAccessor(i));
        result.push(buildAccessor(i));
    }

    return result;
}

var a = ["a", 24, {
    foo: "bar"
}];
var b = transform(a);

console.log(a[1]); // 24
a[1] = "Hello";
console.log(a[1]); // Hello
console.log(b[1]()); // 24 or Hello