I need to add a new property called as income and add the income value that I passed in addIncome(). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I need to add a new property called as income and add the income value that I passed in addIncome().

const account = { Name: 'Sheeba', expenses:[], addExpense:function(description, amount){ this.expenses.push({description:description,amount:amount}) }, addIncome:function(i){ this.expenses.forEach(function(item,i){ item.income = i }) } } account.addExpense('Coffee Day',250) account.addExpense('Starbucks',350) account.addIncome(9000) account.addIncome(7000) console.log(account.expenses). But I am getting 0,1 as the income value because the index value is considered. My income value is not added. How to add it ? [ { description: 'Coffee Day', amount: 250, income: 0 }, { description: 'Starbucks', amount: 350, income: 1 } ]

5th Oct 2021, 10:26 AM
Levi
Levi - avatar
3 Answers
+ 3
If I right undestood what you want then the solution will be here: https://code.sololearn.com/W6cRO2qm3O8x/?ref=app
5th Oct 2021, 7:43 PM
JaScript
JaScript - avatar
+ 2
For this Levi , you should search for right item with a second function parameter, for example description as in above updated code.
6th Oct 2021, 7:29 AM
JaScript
JaScript - avatar
+ 1
JaScript actually I should get [ { description: 'Coffee Day', amount: 250, income: 9000 }, { description: 'Starbucks', amount: 350, income: 7000 } ]
6th Oct 2021, 6:58 AM
Levi
Levi - avatar