1

So i'm working on a simple For/in loop exercise from a course i am taking. We have a simple object with 3 properties and we have to create a function that takes 2 parameters - the object name and the item you're looking for.

I made my function and compared to the solution from the teacher and it's exactly the same. The problem is when i try it out in the console, i get an error that accuses the object property is not defined.

The code is as follows:

// Question #2:
// Write a function checkBasket() that lets you know if the item is in the basket or not
const amazonBasket = {
  glasses: 1,
  books: 2,
  floss: 100
}

function checkBasket(basket, lookingFor) {
  for(item in basket) {
    console.log(item);
     if(item === lookingFor) {
      return `${lookingFor} is in your basket`;
    }     
   }
   return `${lookingfor} is not in your basket`;
}

Would really appreciate any help guys! It's been a tough but enjoyable learning process!

Thanks!

3
  • You have to call the function! Commented Feb 14, 2019 at 13:05
  • 1
    you have a undefined variable lookingfor in you last return you need lookingFor Commented Feb 14, 2019 at 13:09
  • Hey guys, appreciate the input, but the problem was that when i was calling the function, i was not writing the item between quotes! :P Silly mistake, but the lecture really got us confuse with this exercise hehe thanks for the help! Commented Feb 14, 2019 at 14:10

3 Answers 3

2

Your code works as expected for me, so the issue is probably with how you call the function or something in code not shown.

PS: If you use an array instead of an object to store the items, you can use array.find() or array.indexOf() and such to make manipulating the basket easier.

// Question #2:
// Write a function checkBasket() that lets you know if the item is in the basket or not
const amazonBasket = {
  glasses: 1,
  books: 2,
  floss: 100
}

function checkBasket(basket, lookingFor) {
  for(item in basket) {
    console.log(item);
     if(item === lookingFor) {
      return `${lookingFor} is in your basket`;
    }     
   }
   return `${lookingFor} is not in your basket`;
}

console.log( checkBasket( amazonBasket, 'floss' ));
console.log( checkBasket( amazonBasket, 'books' ));
console.log( checkBasket( amazonBasket, 'glasses' ));

const amazonBasket = [
  { "order_id": 1, "product_id": 13341544, "product_name": "glasses", "quantity": 1 },
  { "order_id": 1, "product_id": 12121321, "product_name": "books", "quantity": 5 },
  { "order_id": 1, "product_id": 47254114, "product_name": "floss", "quantity": 100 }
];

const checkBasket = ( basket, lookingFor ) => {
  const item = basket.find( item => item.product_name === lookingFor );
  if ( item ) return `${lookingFor} is in your basket`;
  else return `${lookingFor} is not in your basket`;
};

console.log( checkBasket( amazonBasket, 'floss' ));
console.log( checkBasket( amazonBasket, 'books' ));
console.log( checkBasket( amazonBasket, 'glasses' ));

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time! Your code/test made me realize i made the stupid mistake of not including quotation marks around the lookingFor item when calling the function :P Thank you for the help!
1

Your code have a typo in last line: lookingfor instead of lookingFor

1 Comment

Thanks for spotting that, Alexey! Turns out the problem was also calling the function. I was not including the lookingFor item into quotation marks when calling the function! :P
0

Just got to this section of the course. The object should be a string. Try it this way, after putting the code, call the function like this:

checkBasket(amazonBasket, 'books')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.