Skip to content

Instantly share code, notes, and snippets.

@blak3r
Created November 9, 2013 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blak3r/7380034 to your computer and use it in GitHub Desktop.
Save blak3r/7380034 to your computer and use it in GitHub Desktop.
Javascript interview question.
/**
* Takes in an array of two objects, flattens the currArray, and prints an HTML Table of the values.
* The HTML table has a column header which is a superset of all keys in all the objects.
* Any values that have changed (ie field value changed or is a new key altogether) should be bolded.
*
* @param prevArray is an array of objects
* @param currArray is an array of objects
* @return a string with HTML markup in it, should return null if error occurs.
*/
function arrayDiffToHtmlTable( prevArray, currArray) {
// Note: you cannot depend solely on array index for comparison.
// The currArray could have more or potentially even be in a different order.
// However, you can assume that each item in the arrays will have an "_id" parameter
//
// Example, Given the following data set:
//
// var prevArray = [ {_id:1, someKey: "RINGING", meta: { subKey1: 1234, subKey2: 52 } } ];
// var currArray = [ {_id:1, someKey: "HANGUP", meta: { subKey1: 1234, subKey2: 56 } },
// {_id:2, someKey: "RINGING", meta: { subKey1: 5678, subKey2: 207, subKey3: 52 } } ];
//
// console.log( arrayDiffToHtmlTable( prevArray, currArray));
//
// OUTPUT (Note this is a text representation... output should be an HTML table):
//
// _id someKey meta_subKey1 meta_subKey2 meta_subKey3
// 1 **HANGUP** 1234 **56**
// **2** **RINGING** **5678** **207** **52**
//
// ** implies this field should be bold or highlighted.
// !!! analyze the example carefully as it demonstrates expected cases that need to be handled. !!!
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment