Exercise - Data Grid (Basic)

by Jagadeesh Vallabhaneni

JavaScript

/**
 * Grid Builder
 * Create a function "gridify" that accepts an array of similar objects
 * It will output a "table" in the console
 * The header row will be the property names and should display ONCE
 * The body rows will be the property values
 *
 * Hint: use tabs to align, "\t" is the tab character
 *
 */

/* your code here */

gridify([{
    name: "Ryan",
    value: 913
}, {
    name: "Jimmy",
    value: 20003
}, {
    name: "Donna",
    value: 923
}]);

function gridify(arr) {
    var fobj = arr[0];
    var header = "";
    for (var key in fobj) {
         header += key.substr(0,1).toUpperCase()+key.substr(1);
        header += "\t";
    }
   console.log(header);
    var len = arr.length;
    for (var i=0; i<len; i++) {
        var elem = "";
         fobj = arr[i];
        for ( key in fobj) {
           elem += fobj[key];
            elem += "\t";
        }
        console.log(elem);
    }
}

/*
ex output:
Name      Value
Ryan      913
Jimmy     20003
Donna     923
*/