Skip to content

Instantly share code, notes, and snippets.

@vre2h
Created January 6, 2021 17:39
Show Gist options
  • Save vre2h/09777c35a2db6247749e38f8b0898d3f to your computer and use it in GitHub Desktop.
Save vre2h/09777c35a2db6247749e38f8b0898d3f to your computer and use it in GitHub Desktop.

Homework Tasks

Map

Your function must use map method

  • Write a function which returns array of usernames.

    const users = [
      {
        username: "Yuri Gagarin",
        lang: "ru",
      },
      {
        username: "Nil Armstrong",
        lang: "ENG",
      },
    ];
    getUserNames(users); // ['Yuri Gagarin', 'Nil Armstrong']
  • Write a function which returns array of lengths of user names

    const users = [
      {
        username: "Yuri Gagarin",
        lang: "ru",
      },
      {
        username: "Nil Armstrong",
        lang: "ENG",
      },
    ];
    getUsernameLengths(users); // [12, 13]
    • Write a function which parses string integers. If it's not possible to parse, then add null
    parseInteger(["1", "2", "34"]); // [1, 2, 34];
    parseInteger(["1", "px", "2323"]); // [1, null, 2323];

Filter

Your functions must use filter method

  • Write a function which remove users with language equals to 'ru'.

    const users = [
      {
        username: "Yuri Gagarin",
        lang: "ru",
      },
      {
        username: "Nil Armstrong",
        lang: "ENG",
      },
    ];
    filterUsers(users); // [{ username: "Nil Armstrong, lang: "ENG" }]
    • Write a function which filters object by field.
    const users = [
      {
        username: "Yuri Gagarin",
        lang: "ru",
        isAstronaut: true,
      },
      {
        username: "Nil Armstrong",
        lang: "ENG",
        isAstronaut: true,
      },
      {
        username: "Elon Musk",
        isAstronaut: false,
      },
    ];
    filterByField(users, "isAstronaut"); // [{ username: "Yuri Gagarin", lang: "ru", isAstronaut: true, }, { username: "Nil Armstrong, lang: "ENG" }]

Reduce

Your function must use reduce

  • Write a function which calculates average age of users.

    const users = [
      {
        username: "Yuri Gagarin",
        lang: "ru",
        age: 56,
      },
      {
        username: "Nil Armstrong",
        lang: "ENG",
        age: 54,
      },
    ];
    getAverageAge(users); // 55

Methods

  • *Implement these array methods
    • forEach
    • filter
    • map
    • every, once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment