Ask coding questions

← Back to all posts
Better Grasp of Understanding
AngelaThomas2 (3)

I have been meeting some difficulty with the lines of code on this repl:

https://repl.it/@AngelaThomas2/allCandyOrders .

When I run the code, it says I am passing half of what is expected, but failing on returning {} when called with []. Why might I be getting this error and how can I solve it out? I have gone back through this scenario and its entirety and can't find the missing piece of this puzzle.

Commentshotnewtop
malvoliothegood (696)

It took some time to understand what was going on with your code. I have forked your program and edited the code so it works, and more importantly that I understand how it works. See if it is useful to you: https://repl.it/@malvoliothegood/allCandyOrders-1

AngelaThomas2 (3)

@malvoliothegood Whereas I do appreciate you helping me crack this, I don't think I can change the test code at the bottom to affect how the code turns out. Also, as it stands I have not worked with "const" before. Is that the same as "let" or "function"? I've been playing with a couple things to get my answer and haven't quite cracked it. I am still trying to write in the lines necessary to return {} when called with []. Have any clue on how I might do that? My latest solution of calling an empty array did not work.

malvoliothegood (696)

@AngelaThomas2 let and const are recent additions to the JS language. It is better to use them instead of var because they give better protection to your code. Basically I always use const unless the value of a variable is likely to change, then I use let.

const animals = ['horse', 'pig', 'cow'];
// let is used to declare i as the value of i changes
for (let i = 0; i < animals.length; i++) {
  console.log('Hello ' + animals[i]);
}

Read more about var, let and const in this article: https://tylermcginnis.com/var-let-const/

I will look at your code again later today. I am guessing you were given boilerplate code to use by your teacher.

malvoliothegood (696)

@AngelaThomas2 I have had a look at your latest code and it doesn't really make sense. What is the allCandyOrders function meant to do? I.e. what written instructions have you been given? I am thinking that you don't really understand what is going on.

From the function's name, allCandyOrders, and the fact that it has a parameter called inventory I am surmising that it takes in an inventory and returns the items in it that are candies. Instead you have hard-coded and returned an object that does not have the same keys that your inventory object in index.js has. Presumably you have done this so it will pass the test.

It is also confusing that you have two inventory arrays, one in each file.

test(allCandyOrders([]), {}, '[]') is failing because allCandyOrders returns the object that you have hard-coded in no matter what argument you pass into it. Therefore the comparison that the test function does is between this object and an empty object, so of course it fails. For the test to pass both the value that allCandyOrders returns and the value you are testing have to be the same in regards to the key names. The values each key has do not matter so much.

I hope this feedback has been of help to you.