This repository contains solutions for 3 tasks focused on arrays and functions in JavaScript. Each task is implemented in a separate file (task-1.js, task-2.js, task-3.js).
A function was written to generate a slug from a given string.
Slug rules:
- All characters must be lowercase.
- Words must be joined with a dash (
-).
function slugify(title) {
return title.toLowerCase().split(" ").join("-");
}
✅ Test Results
slugify("Arrays for begginers");
// "arrays-for-begginers"
slugify("English for developer");
// "english-for-developer"
slugify("Ten secrets of JavaScript");
// "ten-secrets-of-javascript"
slugify("How to become a JUNIOR developer in TWO WEEKS");
// "how-to-become-a-junior-developer-in-two-weeks"
A function that merges two arrays and limits the result by maxLength.
function makeArray(firstArray, secondArray, maxLength) {
const newArray = firstArray.concat(secondArray);
if (newArray.length > maxLength) {
newArray.length = maxLength;
}
return newArray;
}
✅ Test Results
makeArray(["Mango", "Poly"], ["Ajax", "Chelsea"], 3);
// ["Mango", "Poly", "Ajax"]
makeArray(["Mango", "Poly", "Houston"], ["Ajax", "Chelsea"], 4);
// ["Mango", "Poly", "Houston", "Ajax"]
makeArray(["Mango"], ["Ajax", "Chelsea", "Poly", "Houston"], 3);
// ["Mango", "Ajax", "Chelsea"]
makeArray(["Earth", "Jupiter"], ["Neptune", "Uranus"], 2);
// ["Earth", "Jupiter"]
makeArray(["Earth", "Jupiter"], ["Neptune", "Uranus"], 4);
// ["Earth", "Jupiter", "Neptune", "Uranus"]
makeArray(["Earth", "Jupiter"], ["Neptune", "Uranus", "Venus"], 0);
// []
A function that filters numbers from an array, keeping only those greater than a given value.
function filterArray(numbers, value) {
const newArray = [];
for (const number of numbers) {
if (number > value) {
newArray.push(number);
}
}
return newArray;
}
✅ Test Results
filterArray([1, 2, 3, 4, 5], 3);
// [4, 5]
filterArray([1, 2, 3, 4, 5], 4);
// [5]
filterArray([1, 2, 3, 4, 5], 5);
// []
filterArray([12, 24, 8, 41, 76], 38);
// [41, 76]
filterArray([12, 24, 8, 41, 76], 20);
// [24, 41, 76]
Great progress! 🎉 With this homework, you strengthened your skills in arrays and functions.
Now you know:
- How arrays work
- Basic array methods
- Iteration with
for...ofloop - Writing and using functions effectively
- The difference between global scope and block scope
It’s time to reinforce this knowledge with practice and keep climbing higher 🚀
- Task 1: Slug creation from strings
- Task 2: Array composition with
concatandmaxLengthlimit - Task 3: Filtering numbers greater than a given value