Skip to content
Exercise · Array methods · lesson 11 of 12

Exercise: checking ages

Write two functions for an array of ages ages:

  • function firstAdult(ages, minAge) returns the first age, in order, that is not less than minAge, the age of majority, which differs between countries;
  • function allTeens(ages) returns true if every age is from 13 to 19 inclusive, and false otherwise.

Examples:

  • firstAdult([12, 17, 21, 30], 18) → 21
  • firstAdult([16, 19, 25], 21) → 25
  • firstAdult([5, 10, 17], 18) → undefined
  • allTeens([13, 15, 19]) → true
  • allTeens([14, 20, 16]) → false

Constraints: ages holds 0 to 10 integers from 0 to 120, and minAge is an integer from 14 to 21; if no age fits, an empty array included, firstAdult returns undefined; for an empty array allTeens returns true.

Do not add extra imports unless the task says so explicitly.

ages.js — javascript
javascript · 8 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way