Shopping List exercise (trouble in Task 4)

Hi everyone!

I’m new to exercism and I’ve started with the jq programming course.

I was really stuck in the Shopping List exercise (just the second, after the ‘Hello world’). But finally I found the way.

Now I have a solution for the Task 4 (the rest of the exercise is marked as OK) that the ‘exercism editor’ says is incorrect.

I don’t know if I can share my code, but the result of it in a CLI editor with jq is:

{
  "blueberries": "chopped apple",
  "buttermilk": "regular milk",
  "melted butter": "vegetable oil"
}

while the desired result is:

{"buttermilk":"regular milk","melted butter":"vegetable oil","blueberries":"chopped apple"}

Obviously, only the order of items in the object change.

Is my solution well-done or wrong? How could I advance on the exercises?

Thank you

Thanks for the feedback. I see your point. The comparison should be done without taking into consideration the order of the object keys.

Can you show your code for task 4?

Alternately, start a mentoring session.

Here is the code I’ve used

([((."optional ingredients"+.ingredients)[])|select(has("substitute"))]|map( { (.item): .substitute } ) | add  )

That’s all good. I think the author of the tests (ahem, me) expected you to write (.ingredients + ."optional ingredients"), but that should not matter.

I’m creating a fix, but in the meantime, to get your code passing, switch the order of the arrays.

1 Like

Thank you very much.

With that change the code passes all tests.

I would encourage you to add some more whitespace, for readability. This is a style I like:

(
  [
    (.ingredients + ."optional ingredients")[]
    | select(has("substitute"))
  ]
  | map( { (.item): .substitute } ) 
  | add
)

I’ve made a new iteration with the code rewritten in better style mode.
I hope it will gain in readability and clarity.
Thanks again

The fix has been deployed.

This code

# Task 4: map the listed substitutions
(
  ."optional ingredients" + .ingredients
  | map(select(has("substitute")) | {(.item): .substitute})
  | add
  | debug
)

Passes the tests with this information:

CODE RUN

run jq -c -f shopping.jq shopping-list.json
assert_success
assert_objects_equal \
    "${lines[3]}" \
    '{
        "buttermilk":"regular milk",
        "melted butter":"vegetable oil",
        "blueberries":"chopped apple"
     }'
YOUR OUTPUT

["DEBUG:",{"blueberries":"chopped apple","buttermilk":"regular milk","melted butter":"vegetable oil"}]