How to explain the function what value we passed?

Object passed this type

obj = { "Апельсины": {"price": 4,"kolvo": 2},"Бананы": {"price": 2,"kolvo": 0} } 

But will bring the result with the price and quantity

 var items = { "Апельсины": { "price": 4, "kolvo": 2 }, "Бананы": { "price": 2, "kolvo": 0 } }; notification(items, (kolvo) => kolvo === 2) function notification(items, condition) { for (const name in items) { let price = items[name].price let kolvo = items[name].kolvo if (condition(price)) { console.log(name + price) } else if (condition(kolvo)) { console.log(name + kolvo) } } } 

  • Specify the question, what do you want to see as a result of the function? - meine

2 answers 2

No It is necessary to have two functions - one checks the condition for the price, and the second for the quantity.

If you want to return, then you can pass an additional parameter, either with the name of the field, or just a flag. But it is uncomfortable and wrong.

     var items = { "Апельсины": { "price": 4, "kolvo": 2 }, "Бананы": { "price": 2, "kolvo": 0 } }; // check price notification(items, item => item.price === 2); // check quantity notification(items, item => item.kolvo === 2); function notification(items, condition) { for (const name in items) { if (condition(items[name])) { console.log(name, JSON.stringify(items[name])); } } } 

     var items = { "Апельсины": { "price": 4, "kolvo": 2 }, "Бананы": { "price": 2, "kolvo": 0 } }; notification(items, objCheck => { for(var prop in objCheck) return objCheck[prop] === 2; }); function notification(items, condition) { for (const name in items) { var price = items[name].price; if (condition({price})) { console.log(name, JSON.stringify(items[name])); } } } 

    • Think he needs 2 call notification? - Qwertiy
    • @Qwertiy can - yes, maybe - no. The point is to transfer the validation logic to a parameter function. - Igor
    • @lgor thanks for the help, but Qwertiy is right. Trying to implement with one call. - NyanSoldier 4:44 pm
    • @NyanSoldier Comment out one of the challenges - there will be one. - Igor