4 ms·
What is the most difficult thing you’ve gotten it to do?
by jprete 3y ago
What is the most difficult thing you’ve gotten it to do?
- ramesh31 3y ago>What is the most difficult thing you’ve gotten it to do? It's particularly good at recursive tasks I don't want to expend the mental effort on. I.e: Llama: I am Llama, a programming assistant. Please provide me with a task. User: Thanks. Here is a data structure. Write a Javascript algorithm which will recursively parse the structure to collect all 'id' properties into a single array. Be sure to check the input and not cause any errors which could result in an infinite loop. ``` const data = { 'id': 'test1', 'nodes': [{ 'data': {}, 'list': [{ 'id': 'test2', 'values': [{ 'id': 'test3', 'name': 'val' }] }] }] } ``` Llama: Sure thing! Here is the algorithm in Javascript: ``` function parseId(data) { const idArray = []; // Recursively traverse the data structure function traverse(data) { Object.keys(data).forEach((key) => { if (key === 'id') { idArray.push(data[key]); } else if (typeof data[key] === 'object') { traverse(data[key]); } }); } // Start the recursion traverse(data); return idArray; } parseId(data) // Output: ["test1","test2","test3"] ```
- deleted 3y ago[deleted]