Practice

Test your JavaScript skills with these practice tasks (Codewars-style).

Task 1: Divisible Numbers

Write a function that returns an array of all numbers from 1 to n that are divisible by either 3 or 5.

function divisible(n) {
  return Array.from({ length: n }, (_, i) => i + 1)
    .filter(x => x % 3 === 0 || x % 5 === 0)
}

divisible(15)
// [3, 5, 6, 9, 10, 12, 15]

Task 2: Age Calculator

Given a birth year, calculate a person's age.

function calcAge(birthYear) {
  return new Date().getFullYear() - birthYear
}

calcAge(2000) // 24 (in 2024)

Task 3: Odd Number Counter

Count how many odd numbers are in an array.

function countOdd(arr) {
  return arr.filter(n => n % 2 !== 0).length
}

countOdd([1, 2, 3, 4, 5]) // 3

Task 4: String to Number Sum

Given a string of space-separated numbers, return their sum.

function sumString(str) {
  return str.split(' ')
    .map(Number)
    .reduce((sum, n) => sum + n, 0)
}

sumString('1 2 3 4 5') // 15

Challenge yourself on Codewars

Practice more at codewars.com — search for JavaScript katas to sharpen your skills.