Skip to content

TypeScript

sakib03 edited this page Feb 24, 2024 · 18 revisions

Important Links

DOM Manipulation

Hello World in TypeScript

How To Use Decorators in TypeScript


YouTube Links

ES6 and Typescript Tutorial

Learn TypeScript in 50 Minutes - Tutorial for Beginners


Logical Question

Write some code for a basic class in TypeScript with a constructor and a method.

Write a function for the following

Question

/* 
given list of States and Counties
*/
const countyByState = [
    {
        County: 'Autauga County',
        State: 'Alabama',
    },
    {
        County: 'Baldwin County',
        State: 'Alabama',
    },
    {
        County: 'Barbour County',
        State: 'Alabama',
    },
    {
        County: 'Bibb County',
        State: 'Alabama',
    },
    {
        County: 'Blount County',
        State: 'Alabama',
    },
    {
        County: 'Bullock County',
        State: 'Alabama',
    },
    {
        County: 'Butler County',
        State: 'Alabama',
    },
    {
        County: 'Calhoun County',
        State: 'Alabama',
    },
    {
        County: 'Chambers County',
        State: 'Alabama',
    },
    {
        County: 'Cherokee County',
        State: 'Alabama',
    },
    {
        County: 'Chilton County',
        State: 'Alabama',
    },
    {
        County: 'Choctaw County',
        State: 'Alabama',
    },
    {
        County: 'Clarke County',
        State: 'Alabama',
    },
    {
        County: 'Clay County',
        State: 'Alabama',
    },
    {
        County: 'Cleburne County',
        State: 'Alabama',
    },
    {
        County: 'Coffee County',
        State: 'Alabama',
    },
    {
        County: 'Colbert County',
        State: 'Alabama',
    },
    {
        County: 'Conecuh County',
        State: 'Alabama',
    },
    {
        County: 'Coosa County',
        State: 'Alabama',
    },
    {
        County: 'Covington County',
        State: 'Alabama',
    },
    {
        County: 'Crenshaw County',
        State: 'Alabama',
    },
    {
        County: 'Cullman County',
        State: 'Alabama',
    },
    {
        County: 'Dale County',
        State: 'Alabama',
    },
    {
        County: 'Dallas County',
        State: 'Alabama',
    },
    {
        County: 'DeKalb County',
        State: 'Alabama',
    },
    {
        County: 'Elmore County',
        State: 'Alabama',
    },
    {
        County: 'Escambia County',
        State: 'Alabama',
    },
    {
        County: 'Etowah County',
        State: 'Alabama',
    },
    {
        County: 'Fayette County',
        State: 'Alabama',
    },
    {
        County: 'Franklin County',
        State: 'Alabama',
    },
    {
        County: 'Geneva County',
        State: 'Alabama',
    },
    {
        County: 'Greene County',
        State: 'Alabama',
    },
    {
        County: 'Hale County',
        State: 'Alabama',
    },
    {
        County: 'Henry County',
        State: 'Alabama',
    },
    {
        County: 'Houston County',
        State: 'Alabama',
    },
    {
        County: 'Jackson County',
        State: 'Alabama',
    },
    {
        County: 'Jefferson County',
        State: 'Alabama',
    },
    {
        County: 'Lamar County',
        State: 'Alabama',
    },
    {
        County: 'Lauderdale County',
        State: 'Alabama',
    },
    {
        County: 'Lawrence County',
        State: 'Alabama',
    },
    {
        County: 'Lee County',
        State: 'Alabama',
    },
    {
        County: 'Limestone County',
        State: 'Alabama',
    },
    {
        County: 'Lowndes County',
        State: 'Alabama',
    },
    {
        County: 'Macon County',
        State: 'Alabama',
    },
    {
        County: 'Madison County',
        State: 'Alabama',
    },
    {
        County: 'Marengo County',
        State: 'Alabama',
    },
    {
        County: 'Marion County',
        State: 'Alabama',
    },
    {
        County: 'Marshall County',
        State: 'Alabama',
    },
    {
        County: 'Mobile County',
        State: 'Alabama',
    },
    {
        County: 'Monroe County',
        State: 'Alabama',
    },
    {
        County: 'Montgomery County',
        State: 'Alabama',
    },
    {
        County: 'Morgan County',
        State: 'Alabama',
    },
    {
        County: 'Perry County',
        State: 'Alabama',
    },
    {
        County: 'Pickens County',
        State: 'Alabama',
    },
    {
        County: 'Pike County',
        State: 'Alabama',
    },
    {
        County: 'Randolph County',
        State: 'Alabama',
    },
    {
        County: 'Russell County',
        State: 'Alabama',
    },
    {
        County: 'St. Clair County',
        State: 'Alabama',
    },
    {
        County: 'Shelby County',
        State: 'Alabama',
    },
    {
        County: 'Sumter County',
        State: 'Alabama',
    },
    {
        County: 'Talladega County',
        State: 'Alabama',
    },
    {
        County: 'Tallapoosa County',
        State: 'Alabama',
    },
    {
        County: 'Tuscaloosa County',
        State: 'Alabama',
    },
    {
        County: 'Walker County',
        State: 'Alabama',
    },
    {
        County: 'Washington County',
        State: 'Alabama',
    },
    {
        County: 'Wilcox County',
        State: 'Alabama',
    },
    {
        County: 'Winston County',
        State: 'Alabama',
    },
    {
        County: 'Aleutians East Borough',
        State: 'Alaska',
    },
    {
        County: 'Aleutians West Census Area',
        State: 'Alaska',
    },
    {
        County: 'Anchorage, Municipality of',
        State: 'Alaska',
    },
    {
        County: 'Bethel Census Area',
        State: 'Alaska',
    },
    {
        County: 'Bristol Bay Borough',
        State: 'Alaska',
    },
    {
        County: 'Denali Borough',
        State: 'Alaska',
    },
    {
        County: 'Dillingham Census Area',
        State: 'Alaska',
    },
    {
        County: 'Fairbanks North Star Borough',
        State: 'Alaska',
    },
    {
        County: 'Haines Borough',
        State: 'Alaska',
    },
    {
        County: 'Hoonah-Angoon Census Area',
        State: 'Alaska',
    },
    {
        County: 'Juneau, City and Borough',
        State: 'Alaska',
    },
    {
        County: 'Kenai Peninsula Borough',
        State: 'Alaska',
    },
    {
        County: 'Ketchikan Gateway Borough',
        State: 'Alaska',
    },
    {
        County: 'Kodiak Island Borough',
        State: 'Alaska',
    },
    {
        County: 'Lake and Peninsula Borough',
        State: 'Alaska',
    },
    {
        County: 'Matanuska-Susitna Borough',
        State: 'Alaska',
    },
    {
        County: 'Nome Census Area',
        State: 'Alaska',
    },
    {
        County: 'North Slope Borough',
        State: 'Alaska',
    },
    {
        County: 'Northwest Arctic Borough',
        State: 'Alaska',
    },
    {
        County: 'Petersburg Census Area',
        State: 'Alaska',
    },
    {
        County: 'Prince of Wales-Hyder Census Area',
        State: 'Alaska',
    },
    {
        County: 'Sitka, City and Borough of',
        State: 'Alaska',
    },
    {
        County: 'Skagway Borough, Municipality of',
        State: 'Alaska',
    },
    {
        County: 'Southeast Fairbanks Census Area',
        State: 'Alaska',
    },
    {
        County: 'Valdez-Cordova Census Area',
        State: 'Alaska',
    },
    {
        County: 'Wade Hampton Census Area',
        State: 'Alaska',
    },
    {
        County: 'Wrangell, City and Borough of',
        State: 'Alaska',
    },
    {
        County: 'Yakutat, City and Borough of',
        State: 'Alaska',
    },
    {
        County: 'Yukon-Koyukuk Census Area',
        State: 'Alaska',
    },
    {
        County: 'Apache County',
        State: 'Arizona',
    },
    {
        County: 'Cochise County',
        State: 'Arizona',
    },
    {
        County: 'Coconino County',
        State: 'Arizona',
    },
    {
        County: 'Gila County',
        State: 'Arizona',
    },
    {
        County: 'Graham County',
        State: 'Arizona',
    },
    {
        County: 'Greenlee County',
        State: 'Arizona',
    },
    {
        County: 'La Paz County',
        State: 'Arizona',
    },
    {
        County: 'Maricopa County',
        State: 'Arizona',
    },
    {
        County: 'Mohave County',
        State: 'Arizona',
    },
    {
        County: 'Navajo County',
        State: 'Arizona',
    },
    {
        County: 'Pima County',
        State: 'Arizona',
    },
    {
        County: 'Pinal County',
        State: 'Arizona',
    },
    {
        County: 'Santa Cruz County',
        State: 'Arizona',
    },
    {
        County: 'Yavapai County',
        State: 'Arizona',
    },
    {
        County: 'Yuma County',
        State: 'Arizona',
    },
    {
        County: 'Arkansas County',
        State: 'Arkansas',
    },
    {
        County: 'Ashley County',
        State: 'Arkansas',
    },
    {
        County: 'Baxter County',
        State: 'Arkansas',
    },
    {
        County: 'Benton County',
        State: 'Arkansas',
    },
    {
        County: 'Boone County',
        State: 'Arkansas',
    },
    {
        County: 'Bradley County',
        State: 'Arkansas',
    },
    {
        County: 'Calhoun County',
        State: 'Arkansas',
    },
    {
        County: 'Carroll County',
        State: 'Arkansas',
    },
    {
        County: 'Chicot County',
        State: 'Arkansas',
    },
    {
        County: 'Clark County',
        State: 'Arkansas',
    },
    {
        County: 'Clay County',
        State: 'Arkansas',
    },
    {
        County: 'Cleburne County',
        State: 'Arkansas',
    },
    {
        County: 'Cleveland County',
        State: 'Arkansas',
    },
    {
        County: 'Columbia County',
        State: 'Arkansas',
    },
];

const getCounties = (state: string) => {
    return 'O(1)';
};

/*
* should display list of counties
*/
console.log(getCounties('Alaska'));

Answer

const getCounties = (state: string) => {
    /*
    * function to display count of counties
    */
    let countyCount = countyByState.filter(
        (countyObj) => countyObj.State === state
    ).length;
    return `${state}(${countyCount})`;

    /*
    * function to display list of counties
    */
    let countyList = countyByState
        .filter((countyObj) => countyObj.State === state)
        .map((countyItem) => countyItem.County)
        .join(", ");
    return `${state}(${countyList})`;
};
console.log(getCounties("Alaska"));

Clone this wiki locally