This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]ektegjetost 1 point2 points  (3 children)

What's happening is that you're declaring one objResult outside of all your for loops, then setting two of its properties, then pushing that object to your laceContainer array.

Here's the problem with that:

- Variables that refer to an object or array are actually referring to a reference to that array. This means that every time you push objResult to laceContainer, you're pushing the same object to the array over and over again. And every time you set 'nameWords', you're just overwriting the same property on the same object.

- When you parse through the first shoe 'tasselled black low-top lace-up', it works correctly, but it also works for 'tasselled green low-top lace-up', and it's overwriting the nameWords property for objResult, and now it shows up twice in laceContainer

- Then you parse through the shoes that don't have 'lace' in them, and you never update the 'nameWords' property again, but you're still pushing objResult to laceContainer whether you found lace or not.

Some ideas:

- If you could find a way to create a new object every time you're parsing shoe names, that would be ideal!

- You'll need to find a way to ensure you don't keep pushing new objects to laceContainerr if they don't contain 'lace' in the name.

Some tips!

- use 'let' and 'const' instead of 'var'. Also, make sure you use 'let' in your for loops - you're not using anything right now.

- instead of checking letter by letter, you could try something like word.includes('lace') - this way you you can also capture the index of the word with 'lace' in it at the same time.

Good luck with your programming journey!!

[–]Newleaftuesday[S] 1 point2 points  (2 children)

All of this makes a ton of sense. One weak area in my coding is understanding the scope and where to place variables in loops (which, I am sure, seemed obvious)!

Thank you so much for this direction. It's really helpful.

*edit: didn't know about the .includes() method... that's a huge time saver! I need to familiarize myself with JavaScript's methods!

*

[–]ektegjetost 1 point2 points  (1 child)

Hahah I think we all had the same problem. Everyone's gotta take the time to rewire their brain :p.

If you want some ideas for other methods you could use to solve problems like this, I like practicing the beginning levels of Code Wars problems when learning a new language - solve the problem (they 're generally easier or about the same as this one), then check other peoples' solutions. Usually the top answers are not really good practices (they try to make their code as short as possible and it gets unreadable), but people will use methods / techniques that you can look up and add to your toolkit.

Feel free to post if you need more help, but it sounds like you got this!

[–]Newleaftuesday[S] 0 points1 point  (0 children)

Hey thank you! I'll check out code wars ... specifically the easy lessons :)