How to push() the result of a function in to an objects property?

How can I push the result of a function into the properties of an object?

See the output below. I’m trying to push() the valueCalc() result to the portfolio object property “value: undefined”

const portfolio = {
    _stocks: [],

    get stock() {
        return this._stocks;
    },

    addStocks(newTicker, newPrice, newQuantity, newValue) {
        let newStock = {
            ticker: newTicker,
            price: newPrice,
            quantity: newQuantity,
            value: newValue
        }
        this._stocks.push(newStock);
    }
};

portfolio.addStocks('PMT', 1.40, 100, );
portfolio.addStocks('PMT', 1.37, 100, );
portfolio.addStocks('PMT', 1.52, 100, );
portfolio.addStocks('WR1', 1.90, 100, );
portfolio.addStocks('WR1', 1.80, 100, );
portfolio.addStocks('WR1', 1.88, 100, );

const valueCalc = () => {
    for (let i = 0; i < portfolio.stock.length; i++) {
        console.log(portfolio.stock[i].price * portfolio.stock[i].quantity)
    }
};

console.log(portfolio.stock);
valueCalc();

OUTPUT:

[
  { ticker: 'PMT', price: 1.4, quantity: 100, value: undefined },
  { ticker: 'PMT', price: 1.37, quantity: 100, value: undefined },
  { ticker: 'PMT', price: 1.52, quantity: 100, value: undefined },
  { ticker: 'WR1', price: 1.9, quantity: 100, value: undefined },
  { ticker: 'WR1', price: 1.8, quantity: 100, value: undefined },
  { ticker: 'WR1', price: 1.88, quantity: 100, value: undefined }
]
140
137
152
190
180
188

You have a stock getter, so why not a setter?

    set stock (x) {
        [t, p, q, v] = x.arguments()
        n = { t: t, p: p, q: q, v: v }
        this._stocks.push(n)
    }

Not tested, and not sure if there is an arguments object in a setter method. This will all fall to you, if you accept the challenge.

1 Like

I believe I’ve solved it. Is this a suitable solution, or is there an alternative?

const portfolio = {
    _stocks: [],

    get stock() {
        return this._stocks;
    },

    valueCalc(stockPrice, stockQuantity) {
        for (let i = 0; i <= this._stocks.length; i++) {
             return (stockPrice * stockQuantity)
        };
    },

    addStocks(newTicker, newPrice, newQuantity, ) {
        let newStock = {
            ticker: newTicker,
            price: newPrice,
            quantity: newQuantity,
            value: this.valueCalc(newPrice, newQuantity)
        }
        this._stocks.push(newStock);
    } 
};

portfolio.addStocks('PMT', 1.40, 100, );
portfolio.addStocks('PMT', 1.37, 100, );
portfolio.addStocks('PMT', 1.52, 100, );
portfolio.addStocks('WR1', 1.90, 100, );
portfolio.addStocks('WR1', 1.80, 100, );
portfolio.addStocks('WR1', 1.88, 100, );

console.log(portfolio.stock);

OUTPUT:

[
  { ticker: 'PMT', price: 1.4, quantity: 100, value: 140 },
  { ticker: 'PMT', price: 1.37, quantity: 100, value: 137 },
  { ticker: 'PMT', price: 1.52, quantity: 100, value: 152 },
  { ticker: 'WR1', price: 1.9, quantity: 100, value: 190 },
  { ticker: 'WR1', price: 1.8, quantity: 100, value: 180 },
  { ticker: 'WR1', price: 1.88, quantity: 100, value: 188 }
]
1 Like

Good job. Stay the challenge for another day, but do bookmark it so you come back to explore destructuring assignments and fix my code, if needs be.


Well, if I’d thought for a minute earlier I’d have known there is no arguments object because a setter can only take one positional argument, period. It would have to be an array to be destructured.

Consequently, we would need to wrtie,

    set stock (x) {
        [t, p, q, v] = x
        n = { t: t, p: p, q: q, v: v }
        this._stocks.push(n)
    }

The assignment would look like this:

portfolio.stock = ['WR1', 1.90, 100, ]

Please test this and see if we’re off our nut. Thanks.

As last note, I would much rather set the ‘v’ at time of instantiation and let the value fluctuate in session. For that I would insert the line,

v = p

before declaring n (or just set it to p instead of vv: p). No side effects, but the position gets filled with a stereotypical value so looks structurally sound. Just saying.

const portfolio = {
    _stocks: [],
    get stock () {
        return this._stocks;
    },
    set stock (x) {
        [t, p, q] = x
        n = { t: t, p: p, q: q, v: p }
        this._stocks.push(n)
    },
    addStock (t, p, q) {
        this.stock = [t, p, q]
    }
}

portfolio.stock = ['WR1', 1.90, 100]
portfolio.stock = ['PMT', 1.52, 100]
portfolio.addStock('PMT', 1.50, 100)  <- preferred
console.log(portfolio.stock)
0: {t: 'WR1', p: 1.9, q: 100, v: 1.9}
1: {t: 'PMT', p: 1.52, q: 100, v: 1.52}
2: {t: 'PMT', p: 1.5, q: 100, v: 1.5}

So it works. Whoohoo! Love it when a plan comes together. Hope you’re excited too by the time you get to this stuff in your learning. The intermediary helper method, addStock is also adding value by being more out front and not assigning to the backing variable but letting the setter do that after it packages the inputs.

One still believes it can be improved along the lines of destructuring assignments, but that is yet to be explored. Care to be on this journey? It’s bound to be a rabbit hole. That’s always fun.


If one vets the inputs to the addStock method it lets the setter focus on the setting, and not the vetting. That’s where the intermediary is most valuable in the overall picture. BUT, just having an addSomething method is too direct because it is interacting with the backing variable, which in my thinking should not be possible or allowed. Make the intermediary use the getter and setter, period. Then only use the intermediary so the the setter and getter are completely insulated (or at least ignored by obscurity).

1 Like