Created
December 28, 2017 19:33
-
-
Save JacksonRMC/e81e98eeb7bed325f732c65dd0782f53 to your computer and use it in GitHub Desktop.
Coding challenge for Realization. Jackson Carter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem 1: | |
// There is a matrix of cells like a spreadsheet - let's call the JavaScript 2-dimensional array holding the data | |
// as allData. | |
// The matrix has n rows and m columns. Column numbers go from 1 to m, row numbers go from 1 to n. | |
// allData[i] gives the row at index i. A cell on row index i, col index j, can be accessed as allData[i][j]. | |
// Column k is the 'Key' column containing strings. Assume the keys are unique for each row. | |
// Rest of the cells can contain numbers or strings. | |
// 1.a: | |
// Write a function getSortedList that will take allData as a parameter and return a JSON that is an array of | |
// {keyValue: rowdata} objects sorted in alphabetical case-insensitive order of keys, | |
// where rowdata is an array of values | |
// in that row. | |
const getSortedList = (allData, k) => { | |
if ( allData.length === 0 ) return; | |
let cellData = {}; | |
for ( let i = 0 ; i < allData.length ; i ++ ){ | |
cellData[allData[i][k]] = allData[i].sort(); | |
} | |
let sortedCellList = Object.keys(cellData).sort(); | |
let final = []; | |
for ( let j = 0 ; j < sortedCellList.length ; j ++ ){ | |
let obj = {}; | |
obj[sortedCellList[j]] = cellData[sortedCellList[j]]; | |
final.push(obj); | |
} | |
return final; | |
} | |
let allData = [ | |
[1,6,3,8,'b'], | |
[1,6,3,8,'a'], | |
[1,6,3,8,'e'], | |
[1,6,3,8,'t'] | |
]; | |
console.log(getSortedList(allData, 4)); | |
let updateArr = [ | |
{keyValue: 'a', colIndex: 3, valueToSet: 'CHANGED'} | |
] | |
// 1.b: | |
// Write a function updateCell that will update the allData matrix as follows. Input is an array of | |
// {keyValue, colIndex, valueToSet} objects. Using the keyValue (to find the row) and the colIndex, | |
// you will update the cell in allData to ‘valueToSet’. Imagine that this input array can have a large number of entries. | |
// So you need to do this efficiently performance- wise. | |
// Note: You are allowed to create any intermediate objects/structures to make your code efficient in performance. | |
// Assume you have all the memory available. | |
const updateCell = (allData, inputArray, k) => { | |
if ( allData.length === 0 ) return; | |
let cellData = {}; | |
for( let i = 0 ; i < allData.length ; i ++ ){ | |
cellData[allData[i][k]] = allData[i]; | |
} | |
for( let j = 0 ; j < inputArray.length ; j ++ ){ | |
cellData[inputArray[j].keyValue][inputArray[j].colIndex] = inputArray[j].valueToSet; | |
} | |
for ( let y = 0 ; y < allData.length ; y ++ ){ | |
allData[y] = cellData[allData[y][k]] | |
} | |
return allData; | |
} | |
console.log(updateCell(allData, updateArr, 4)); | |
// Problem 2: (not related to the above) | |
// There is a library function updateDB (record, updateDBCallback) available to you. | |
// The function takes an input of a single ’record’ and calls updateDBCallback when the update is complete. | |
// You are writing a function updateManyRecords that gets an array of records to update using updateDB. | |
// When all the records in the array have been updated, you need to call 'callback'. Please complete the function. | |
const updateManyRecords = (recordArray, callback) => { | |
if ( recordArray.length === 0 ) return; | |
let counter = 0; | |
for ( let i = 0 ; i < recordArray.length ; i ++ ){ | |
(function(element, i){ | |
updateDB(element, function(value)){ | |
count++; | |
if ( count === recordArray.length ){ | |
callback(); | |
} | |
}) | |
})(recordArray[i], i) //here we will use an IIFE to perform asynchronous methods | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment