Question: In typescript I am trying to create a member class that takes a varying value (between 2018 - 2020) from an arraylist defined later on

In typescript I am trying to create a member class that takes a varying value (between 2018 - 2020) from an arraylist defined later on (such as 2019) and adds a defined number to it (lets say 3). It then takes the difference between the (starting value + 3) and the current actual year and returns the difference based on the current actual year (answer would be 1 because the year is currently 2021 so you would subtract that from 2022 which is 2019+3) so I can output it later in console.log. I will give an example below of how it should look. I just keep getting "undefined" as my output and cant figure out why. While the starting year will change the value added will always only add 3 onto the year.

class Yeardif {

baseyear: string;

constructor(baseyear: string) {

this.baseyear = baseyear;

}

differenceinyears() {

//this is what I cant figure out

}

var yearList = [];

var years = { "baseyear": "2019");

yearList.push(years);

var years = {"baseyear": "2020");

yearList.push(years);

for (var i=0; i<2; i++) {

console.log("difference in years is" + yearList[i].differenceinyears);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!