0

if the function calculateAvgGainLoss response includes avgGain, avgLoss we can get these values like this.

const { avgGain, avgLoss } = calculateAvgGainLoss(prices);

I want to set these values to the variable that defined outside of the function. How can I do that ? Example below.

describe("test", () => {
  let avgGain: number, avgLoss: number;
  it("Calculate average gain & loss", () => {
    const prices = [...];
    /*...This isn't working, has to be changed...*/ { avgGain, avgLoss } = calculateAvgGainLoss(prices);
    expect(avgGain).toBe(0.24);
    expect(avgLoss).toBe(0.1);
  });

});
Caner Sezgin
  • 328
  • 3
  • 16

1 Answers1

2

Beginning the line by a { confuses parsers (is it the beginning of a scope? an object? etc). Just bypass this by wrapping your line with parenthesis:

describe("test", () => {
  let avgGain: number, avgLoss: number;
  it("Calculate average gain & loss", () => {
    const prices = [...];
    ({ avgGain, avgLoss } = calculateAvgGainLoss(prices)); // <--
    expect(avgGain).toBe(0.24);
    expect(avgLoss).toBe(0.1);
  });

});
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84