I can't get JS to run a list of all objects after a loop - any advice?

Hello all,

I’m currently trying to get a list of all candybars in JS within my getCandyBarsByMaker function, however I’m only getting one object played out on return at a time.

You can see that here: https://codepen.io/koeddi/pen/ZErwLZv

Now I tried to fix this problem with an empty array at the beginning and a return at the end which plays out this empty array. You can see it here: https://codepen.io/koeddi/pen/LYQqxaq

Can you tell me if this is correct in theory to run a list of all objects?

Thanks in advance!

We will need to see the data you are trying to access or at least a sample of it.

1 Like

Okay thanks. Did edit both pens!


In the code provided, maker2 is not defined. What is it meant to be assigned to?

var candyBars = [createCandyBar(‘Kit Kat’, ‘Nestlé’, 239),
createCandyBar(‘Milka’, ‘Mondelēz’, 530),
createCandyBar(‘Reese’s Peanut Butter Cups’, ‘Hershey’, 232),
createCandyBar(‘Ritter Sport Milk’, ‘Ritter’, 553),
createCandyBar(‘Toblerone’, ‘Mondelēz’, 529),
createCandyBar(‘Twix’, ‘Mars’, 291),
It is supposed to be assigned to the [1] element of every createCandyBar in this object.

Where is this assignment supposed to take place?

I’m still figuring that out. I suppose it is meant to happen before the loop but I’m struggling with the syntax.

I’m not sure that I understand what you are trying to do, but it seems that you want to be able to call your getCandyBarsByMaker(maker2) function with an argument, and get back an array with the candy bar objects made by the maker provided. If that’s the case, your function call would look like this for example:


Your return value expected would be (screenshot from codepen):

Is this correct?

1 Like

I’m trying to get a list of all the manufacturers represented in the object (so far I am unfortunately only getting individual manufacturers).

It was recommended to create an empty array before looping. Then any time a match is found in the loop it adds that match to the array. Once the loop is complete I could then return that array. So I understand the general procedure to solve the problem but not the syntax behind it (sorry I am still a beginner).

1 Like

No worries. I’m still not sure I understand exactly what you want to do, but I’ve whipped up a quick example of some code that should be similar enough to use as a guide. Look it over, play with it, and let me know if you have any questions. Hopefully it will help.

Code Sample
const makeWidget = (name, manufacturer, year, description) => {
  return {
    _name: name,
    _manufacturer: manufacturer,
    _year: year,
    _description: description,
    get name() {
      return this._name;
    get manufacturer() {
      return this._manufacturer;
    get year() {
      return this._year;
    get description() {
      return this._description;
   info: function() {
      return `
Name: ${this.name}
Manufacturer: ${this.manufacturer}
Year Developed: ${this.year}
Description: ${this.description}\n`

const w1 = makeWidget("Wonder Widget", "Willie's Widgets", 2020, "This widget performs wonderous actions wonderously.");
const w2 = makeWidget("Scream Light", "Scream Ltd.", 2022, "This widget turns the lights on in a room when someone screams.");
const w3 = makeWidget("Stinks Outloud", "Peeuuu Corp.", 2018, "This widget blars 'Peeeee-Uuuuuu!' when a foul odor is detected.");

//include all widgets in an array
const widgets = [w1, w2, w3];

//see info from all widgets
widgets.forEach(w => console.log(w.info()));

//function to get all manufacturers of widgets
function whoMakesWidgets(widgetArray) {
  let result = [];
  for (let i = 0; i < widgetArray.length; i++) {
  return result;

//call the whoMakesWidgets function 
1 Like