Created
August 19, 2023 18:24
-
-
Save victor-gesit/d30b359b25d404cc6f2e5fde527598e2 to your computer and use it in GitHub Desktop.
InCyan Assessment
This file contains hidden or 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
const plotter = (payload) => { | |
const { title, xtitle, ytitle, items } = payload | |
const titleWidth = title.length | |
const ytitleWidth = ytitle.length | |
let tableHeight = 0 | |
let maxColumnWidth = 0 | |
let restructruedItems = [] | |
items.forEach((item) => { | |
let itemNames = Object.keys(item) | |
if (itemNames && itemNames.length > 0) { | |
const itemName = itemNames[0] | |
let itemCount = item[itemName] | |
tableHeight = itemCount > tableHeight ? itemCount : tableHeight | |
maxColumnWidth = itemName.length > maxColumnWidth ? itemName.length : maxColumnWidth | |
const restructuredItem = { | |
name: itemName, | |
count: itemCount | |
} | |
restructruedItems.push(restructuredItem) | |
} | |
}) | |
maxColumnWidth += 2 // Space between names | |
const titleLeftIndent = 20 | |
const tableLeftIndent = ytitleWidth + 2 | |
console.log(`${" ".repeat(titleLeftIndent)}${title}`) | |
console.log(`${" ".repeat(titleLeftIndent)}${"-".repeat(titleWidth)}`) | |
console.log(`${ytitle}`) | |
for (let i = 0; i < tableHeight; i++) { | |
let entireRowString = " ".repeat(tableLeftIndent) | |
let tableRowString = "" | |
restructruedItems.forEach(({ name, count }) => { | |
const spaceOrStart = tableHeight - i <= count ? "*" : " " | |
tableRowString += `${spaceOrStart}${" ".repeat(maxColumnWidth)}` | |
}) | |
entireRowString += tableRowString | |
console.log(entireRowString) | |
} | |
let xAxisString = " ".repeat(tableLeftIndent) | |
restructruedItems.forEach(({ name }) => { | |
xAxisString += `${name}${" ".repeat(maxColumnWidth - name.length + 1)}` // 1 accounts for spacing between columns | |
}) | |
console.log(xAxisString) | |
const xAxisTitleString = `${" ".repeat(xAxisString.length)} ${xtitle}` | |
console.log(xAxisTitleString) | |
console.log(`${" ".repeat(xAxisString.length + 1)}${"-".repeat(xtitle.length)}`) // account for space between columns | |
} | |
const payload = { | |
"title": "stock count", | |
"xtitle": "asset", | |
"ytitle": "count", | |
"items": [ | |
{ "chairs": 20 }, | |
{ "tables": 5 }, | |
{ "stands": 7 }, | |
{ "lamps": 8 }, | |
{ "cups": 10 } | |
] | |
} | |
plotter(payload) | |
// Running Instructions | |
``` | |
PS: The TypeError on the right is due to the online editor and not an error in the code. | |
Simply click Run above to run the code. | |
Pass in the desired payload into the plotter function call above (line 71) | |
``` | |
// Assumptions | |
``` | |
1. The JSON payload always has a "title", "xtitle", "ytitle" and "items" fields. | |
2. The "items" field is an array of objects, each with a single key and numerical value. | |
3. The keys in the item objects can be any string, and the number of items in the "items" array can be any length. | |
4. The size of the table depends on the highest count in the items array, and the width depends on the number of items in the "items" field. | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment