new class js -advanced
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
const isMixableWithMyIngredients = (cocktailRecipe) =>
|
||||
isMixableWith(cocktailRecipe, ingredientsFromMyBar);
|
||||
|
||||
const isMixableWith = (cocktailRecipe, availableIngredients) =>
|
||||
cocktailRecipe.every((ingredientFromRecipe) =>
|
||||
hasIngredient(availableIngredients, ingredientFromRecipe)
|
||||
);
|
||||
|
||||
const hasIngredient = (listOfIngredients, searchedIngredient) =>
|
||||
listOfIngredients.includes(searchedIngredient);
|
||||
|
||||
const honoluluFlip = [
|
||||
'Maracuja Juice',
|
||||
'Pineapple Juice',
|
||||
'Lemon Juice',
|
||||
'Grapefruit Juice',
|
||||
'Crushed Ice',
|
||||
];
|
||||
const casualFriday = ['Vodka', 'Lime Juice', 'Apple Juice', 'Cucumber'];
|
||||
|
||||
const pinkDolly = [
|
||||
'Vodka',
|
||||
'Orange Juice',
|
||||
'Pineapple Juice',
|
||||
'Grenadine',
|
||||
'Cream',
|
||||
'Coco Syrup',
|
||||
];
|
||||
const cocktailRecipes = [honoluluFlip, casualFriday, pinkDolly];
|
||||
|
||||
const ingredientsFromMyBar = [
|
||||
'Pineapple',
|
||||
'Maracuja Juice',
|
||||
'Cream',
|
||||
'Grapefruit Juice',
|
||||
'Crushed Ice',
|
||||
'Milk',
|
||||
'Vodka',
|
||||
'Apple Juice',
|
||||
'Aperol',
|
||||
'Pineapple Juice',
|
||||
'Lime Juice',
|
||||
'Lemons',
|
||||
'Cucumber',
|
||||
];
|
||||
|
||||
console.log(cocktailRecipes.find(isMixableWithMyIngredients));
|
||||
// => [ 'Vodka', 'Lime Juice', 'Apple Juice', 'Cucumber' ]
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const countriesWithCapital = [
|
||||
['UK', 'London'],
|
||||
['France', 'Paris'],
|
||||
['Germany', 'Berlin'],
|
||||
['Switzerland', 'Bern'],
|
||||
['Austria', 'Vienna'],
|
||||
['Russia', 'Moscow'],
|
||||
];
|
||||
|
||||
const capitalOf = (country) => {
|
||||
const capitalIndex = 1;
|
||||
const countryIndex = 0;
|
||||
return countriesWithCapital.find(
|
||||
(countryWithCapital) => countryWithCapital[countryIndex] === country
|
||||
)[capitalIndex];
|
||||
};
|
||||
|
||||
console.log(capitalOf('Switzerland'));
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const countriesWithCapital = [
|
||||
['UK', 'London'],
|
||||
['France', 'Paris'],
|
||||
['Germany', 'Berlin'],
|
||||
['Switzerland', 'Bern'],
|
||||
['Austria', 'Vienna'],
|
||||
['Russia', 'Moscow'],
|
||||
];
|
||||
|
||||
const countryForCapital = (capital) => {
|
||||
const capitalIndex = 1;
|
||||
const countryIndex = 0;
|
||||
return countriesWithCapital.find(
|
||||
(countryWithCapital) => countryWithCapital[capitalIndex] === capital
|
||||
)[countryIndex];
|
||||
};
|
||||
|
||||
console.log(countryForCapital('Berlin'));
|
||||
@@ -0,0 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
const gradesTable = [
|
||||
['Name', 'Mathematics', 'English', 'Biology', 'History'],
|
||||
['Anna', 85, 92, 78, 88],
|
||||
['Ben', 90, 88, 84, 79],
|
||||
['Clara', 76, 95, 89, 91],
|
||||
['David', 82, 79, 91, 85],
|
||||
];
|
||||
|
||||
// Helper: Calculates average of an array of numbers
|
||||
const getAverage = (numbers) => {
|
||||
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
|
||||
const avg = sum / numbers.length;
|
||||
return Number(avg.toFixed(2));
|
||||
};
|
||||
|
||||
// 1. Calculate average per student
|
||||
const calculateAvgGrades = (table) => {
|
||||
// We take the rows (excluding header), and map each row to a single average value
|
||||
return table.slice(1).map((row) => {
|
||||
const grades = row.slice(1); // Remove student name
|
||||
return getAverage(grades);
|
||||
});
|
||||
};
|
||||
|
||||
// 2. Calculate average per subject
|
||||
const calculateAvgSubjects = (table) => {
|
||||
const headers = table[0].slice(1); // Get subject names ['Mathematics', 'English', ...]
|
||||
const rows = table.slice(1); // Get data rows
|
||||
|
||||
// We map over the headers to create a result for each subject column
|
||||
return headers.map((_, colIndex) => {
|
||||
// For the current column, extract the value from every row
|
||||
// Note: colIndex starts at 0, but in the row data, grades start at index 1
|
||||
const columnGrades = rows.map((row) => row[colIndex + 1]);
|
||||
return getAverage(columnGrades);
|
||||
});
|
||||
};
|
||||
|
||||
// Tests
|
||||
console.log('Student Averages:', calculateAvgGrades(gradesTable));
|
||||
// Expected: [85.75, 85.25, 87.75, 84.25]
|
||||
|
||||
console.log('Subject Averages:', calculateAvgSubjects(gradesTable));
|
||||
// Expected: [83.25, 88.50, 85.50, 85.75]
|
||||
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
const image = [
|
||||
[
|
||||
// Row 1
|
||||
[100, 150, 200], // Pixel 1
|
||||
[50, 100, 150], // Pixel 2
|
||||
],
|
||||
[
|
||||
// Row 2
|
||||
[25, 75, 125], // Pixel 3
|
||||
[200, 225, 250], // Pixel 4
|
||||
],
|
||||
];
|
||||
|
||||
const increaseBrightness = (image, value) => {
|
||||
// Iterate through each line of the image
|
||||
return image.map((row) =>
|
||||
// Iterate through every pixel in the line
|
||||
row.map((pixel) =>
|
||||
// Increase each RGB value by 'value' and make sure that it does not exceed 255
|
||||
pixel.map((component) => Math.min(component + value, 255))
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const newImage = increaseBrightness(image, 30);
|
||||
console.log(newImage);
|
||||
/*
|
||||
Expected outcome:
|
||||
[
|
||||
[
|
||||
[130, 180, 230],
|
||||
[80, 130, 180],
|
||||
],
|
||||
[
|
||||
[55, 105, 155],
|
||||
[230, 255, 255],
|
||||
],
|
||||
]
|
||||
*/
|
||||
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
const inventory = [
|
||||
[
|
||||
// Category: Electronics
|
||||
['Shelf 1', 'Laptop', 10],
|
||||
['Shelf 2', 'Smartphone', 25],
|
||||
],
|
||||
[
|
||||
// Category: Clothing
|
||||
['Shelf 1', 'T-Shirts', 50],
|
||||
['Shelf 2', 'Jeans', 30],
|
||||
],
|
||||
[
|
||||
// Category: Groceries
|
||||
['Shelf 1', 'Milk', 100],
|
||||
['Shelf 2', 'Bread', 80],
|
||||
],
|
||||
];
|
||||
|
||||
// Add new category “Books"
|
||||
inventory.push([
|
||||
['Shelf 1', 'Novels', 40],
|
||||
['Shelf 2', 'Non-fiction', 35],
|
||||
]);
|
||||
|
||||
console.log(inventory);
|
||||
/* Expected outcome:
|
||||
[
|
||||
[ // Electronics
|
||||
['Shelf 1', 'Laptop', 10],
|
||||
['Shelf 2', 'Smartphone', 25],
|
||||
],
|
||||
[ // Clothing
|
||||
['Shelf 1', 'T-Shirts', 50],
|
||||
['Shelf 2', 'Jeans', 30],
|
||||
],
|
||||
[ // Groceries
|
||||
['Shelf 1', 'Milk', 100],
|
||||
['Shelf 2', 'Bread', 80],
|
||||
],
|
||||
[ // Books
|
||||
['Shelf 1', 'Novels', 40],
|
||||
['Shelf 2', 'Non-fiction', 35],
|
||||
],
|
||||
]
|
||||
*/
|
||||
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
const evolutionStages = [
|
||||
['Pidgey', 'Pidgeotto', 'Pidgeot'],
|
||||
['Vulpix', 'Ninetales'],
|
||||
['Dratini', 'Dragonair', 'Dragonite'],
|
||||
];
|
||||
|
||||
const stagesFor = (pokemon) =>
|
||||
evolutionStages.find((stages) => stages.includes(pokemon));
|
||||
|
||||
const stagesAfter = (pokemon) =>
|
||||
stagesFor(pokemon).slice(stagesFor(pokemon).indexOf(pokemon) + 1);
|
||||
|
||||
const stagesBefore = (pokemon) =>
|
||||
stagesFor(pokemon).slice(0, stagesFor(pokemon).indexOf(pokemon));
|
||||
|
||||
console.log(stagesFor('Vulpix')); // => [ 'Vulpix', 'Ninetales' ]
|
||||
console.log(stagesAfter('Dratini')); // => [ 'Dragonair', 'Dragonite' ]
|
||||
console.log(stagesBefore('Pidgeot')); // => [ 'Pidgey', 'Pidgeotto' ]
|
||||
console.log(stagesBefore('Dragonair')); // => [ 'Dratini' ]
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const shoppingList = [
|
||||
['Fruits', 'Apples', 'Bananas', 'Oranges'],
|
||||
['Vegetables', 'Carrots', 'Broccoli', 'Spinach'],
|
||||
['Dairy', 'Milk', 'Cheese', 'Yogurt'],
|
||||
];
|
||||
|
||||
// Your code here
|
||||
shoppingList[0][2] = 'Grapes'; // Replace 'Bananas' with 'Grapes'
|
||||
shoppingList[1][2] = 'Tomatoes'; // Replace 'Broccoli' with 'Tomatoes'
|
||||
shoppingList[2][2] = 'Butter'; // Replace 'Cheese' with 'Butter'
|
||||
|
||||
console.log(shoppingList);
|
||||
// Expected outcome:
|
||||
// [
|
||||
// ['Fruits', 'Apples', 'Grapes', 'Oranges'],
|
||||
// ['Vegetables', 'Carrots', 'Tomatoes', 'Spinach'],
|
||||
// ['Dairy', 'Milk', 'Butter', 'Yogurt'],
|
||||
// ]
|
||||
Reference in New Issue
Block a user