Challenge problem

Where is may fault?

the answer and wanted solution are exactly the same.

Please post a link to this quiz so we can try it out.

I had to alter my code to the recommended one in the solution in order to pass the stage. Next time I will keep my code. Thank you.

1 Like

It would still be nice to look at the exercise. Can you point us to the landing page, please?

There is an internet connection issue tonight. I could not even manage to connect my lesson.

1 Like

Sorry to hear that. Meantime we’ll look over that exercise and give you a brief, presently.

As suspected, the solution does not expect the return values to be in string quotes, since they are already string objects to begin with.

Carrots, Hummus, Pesto and Rigatoni
Bread and Butter
Cheese Balls

I haven’t seen the suggested solution, but here’s a spoiler of mine:

Solution using, `.map()`, `.join()`, `.lastIndexOf()` and `.slice()`
const groceries = u => {
  const m = => x.item)
  if (m.length === 1) return m[0]
  let v = m.join(', ')
  let i = v.lastIndexOf(',')
  return `${v.slice(0, i)} and${v.slice(i + 1)}`

Note that u above is an array of objects. When we map the new array we capture only the item property values. We then join the array to form a string with commas inserted between items (assuming more than one item).

The trick is to find the last comma in the above string. Once that returns an index, we take the slice of everything up to but not including that index, and the slice of everything immediately after that index and interpolate them into the template literal for return.

Thank you for your time.
Roger that…

1 Like