JavaScript bo'yicha misol va masalalar
Vazifa: Shunday funksiya yozingki, arrayni qabul qilib unique raqamlar mavjud bo'lgan array qaytarsin.
function removeDuplicates(arr) {
const seen = new Set();
const result = [];
for (const num of arr) {
if (!seen.has(num)) {
seen.add(num);
result.push(num);
}
}
return result;
}
console.log(removeDuplicates([1,2,2,3,4,4,5]));
[ 1, 2, 3, 4, 5 ]
Yuqoridagi vazifani bundan ham osonroq usul bilan bajarish mumkin. Lekin bu yechim miyani ancha charxlashga va bilimni oshirishga xizmat qiladi.
1. removeDuplicates() funsiyasi yaratildi.
2. Uning ichida esa seen Set'i va result array'i yaratildi.
3. for ... of loop orqali esa arrayni aylanib chiqayapmiz. Bizning maqsad arrayni aylanib chiqib result array'ini to'ldirib qaytarib yuborishdir.
4. loop ichida if blokida !seen.has(num) shartini keltiryapmiz. Ya'ni agar seen Set'i ichida agar raqam bo'lmasa o'shanda loop Set ni ham va result arrayini ham to'ldiradi.
Aytaylik arraydan birinchi bo'lib 1 raqami keldi. if bloki Set ni raqam borligiga tekshiradi. Raqam Set da yo'q ! operatori false ni true ga aylantiradi va Set ga ham result arrayiga ham 1 raqami qo'shiladi. Endi Set ( 1 ) ga va array [ 1 ] ga teng.
Keyin 2 raqami keladi. Set da ham arrayda ham 2 raqami yo'q. Shuning uchun shart bo'yicha u ham ikkalasiga qo'shiladi. Endi arraydagi navbatdagi raqam ham 2 ekan. Bizda Set ( 1, 2 ) teng edi. if blokidagi shart yana Set ni 2 borligiga tekshiradi. 2 bor Set'da. !seen.has(num) sharti true ni false ga aylantiradi. Va blok ichidagi Set ga ham Arrayga ham raqamni qo'shib qo'yish buyruqlarini o'tkazib yuboradi.
Shu tariqa barcha raqamlar tekshirib chiqiladi va array shakllantiriladi.
WordCount
const text = "I love JS because JS is fun";
function wordCount(string) {
const words = string.toLowerCase().split(' ');
for (let word of words) {
map.set(word, (map.get(word) || 0) + 1)
}
return map;
}
Set intersection
// Solution 1: Using Set and filter
function intersection(arr1, arr2) {
const set = new Set(arr1);
const result = arr2.filter(num => set.has(num));
return [...new Set(result)]; // ensure uniqueness
}
// Solution 2: Using Set intersection directly
function intersectionAlt(arr1, arr2) {
const set1 = new Set(arr1);
const set2 = new Set(arr2);
return [...set1].filter(x => set2.has(x));
}
Student score
// Challenge 4: Student Scores with Map
// You are given a list of student-score pairs. Store them in a Map and
// write a function that returns the score of a given student.
// Example:
// const scores = [["Alice", 90], ["Bob", 75], ["Charlie", 85]];
// getScore("Alice") // 90
// getScore("Bob") // 75
const scores = [["Alice", 90], ["Bob", 75], ["Charlie", 85]];
function getScore(name) {
// your code here...
}
console.log(getScore("Alice"));
console.log(getScore("Bob"));
const scores = [["Alice", 90], ["Bob", 75], ["Charlie", 85]];
const scoresMap = new Map(scores);
function getScore(name) {
if (!scoresMap.has(name)) {
return `No score found for ${name}`;
}
return scoresMap.get(name);
}
console.log(getScore("Alice"));
console.log(getScore("Bob"));
console.log(getScore("David"));
Subset Check Set
function isSubset(subset, superset) {
for (let elem of subset) {
if (!superset.has(elem)) {
return false;
}
}
return true;
}
console.log(isSubset(new Set([1,2]), new Set([1,2,3,4])));
console.log(isSubset(new Set([1,5]), new Set([1,2,3,4])));