Challenge 19 Megathread

Welcome to challenge #19!

Use this thread for any and all questions relating to challenge 19

2 Likes

Really ramping it up in these final days.
Great fun, thanks LHL.

After 18 I REALLY over thought this one.
Simplify, test, simplify test, resolve.

Finally the simple and short answer hinged on a very simple question. “Does this exist”

2 Likes

The last challenge really helps to solve this one.

5 Likes

I went back and did some more testing on 18 using the method that solved it for me here.

Now down to 13 lines (11 if I didn’t add a couple to explicitly improve readability)

Refactoring may not be the most glamorous thing to do in code, but it is, on occasion, fulfilling to see the strides you’ve made.

1 Like
const mapped = receivedData.map(item => ({[item.type]: Object.entries(item.data)}));
 
const newObj = Object.assign({}, ...mapped );

return newObj;

This gives me an object but I do not know how to convert this to an array. Can someone please help me?

Very different method to get to a very similar problem.
Have a look at this JSFiddle it will show you how your spread is parsing.
I saw the same behaviour on one of the methods I attempted on this project.

https://jsfiddle.net/bq2ejkr9/
[added some more outputs] Edit fiddle - JSFiddle - Code Playground

Curious how you approached this one… I was having a lot of trouble initially and had to resort to a pretty convoluted series of nested for loops.

Then found a much more useful way to check if a property exists in an object (learned it from the second set of test cases!). This allowed me to pivot and shrink down to a much simpler single loop and if statement.

No spoilers, but these last few challenges seem to have a lot of options on approach, and I’m trying to learn as many new things as I can!

It sounds like we may have settled on a similar final approach after early tribulations.
if (!(objParsedData[(receivedData[i].type)])) {

This line is the crux of my solution.

1 Like

I feel stupid and this is simple code but I watched the video and I am getting the right output. i am stuck on this one:

Hi Joe,

You are very close - don’t feel stupid.

Take a look at this regarding the syntax for your newList: How can I add a key/value pair to a JavaScript object? - Stack Overflow

Also, consider renaming certain things.

I rewrote what you did, made some simple changes, and passed all the tests.

1 Like

I breezed through this one but I hate how this site doesn’t show you the console a lot of the time

2 Likes

Hey there! Console logging works properly, but it doesn’t work in your case since you have a space between log and the opening parenthesis!

ah, that would do it. Thanks!

if you might be so kind as to elaborate on the specifics, as I am quite lost on this one… thank-you in advanced

I renamed the lists you are storing data into (however that was more for readability and it may not impact if the scripts can actually run), I changed the syntax of the if statement (see JavaScript if else else if) and I structured newList’s syntax according to the previous link (How can I add a key/value pair to a JavaScript object? - Stack Overflow).

I’m stuck here and can’t understand why the output of line 18 is 2, {“prop”:[“Saturn Data”]} instead of 2, { astro : [“Saturn Data”]}

image

Copied out your code so I could look at it better.
First thing to note: The expected return should be an object with the data associated to each key being stored as an array.

You’ve hard-coded your key names and arrays, so you may run into issues if any of the test scenarios aren’t exactly what you see in the example input.

In building your array of objects (as opposed to the object of arrays as denoted in first) you call {astro: astro}
How does the JavaScript engine know that the first entry is meant to be a key name, and the second one is supposed to be a variable call, and that both are variable calls?

Because, this is where outputObj is defined. outputObj = {prop: [value]}. It is not a correct way to assign values to an object.
outputObj[prop]=[value] is what you are trying to do here. But your approach still not a good one. It will put only the first data there.
In line 7; you can put a key definition for your outpuObj. such as; outputObj[obj.type] = []
This will create the structure. Another loop can take care of values in the arrays. I’ll leave the rest to you. Hope this helps.

I cannot find any help anywhere on how to manage keys dynamically. How, if I am not hard coding the keys and arrays do I go about defining them

OutputObj[prop] = [value]

1 Like