Last active
January 18, 2022 10:51
-
-
Save sandrabosk/cd9913a6b570ea15a28a2e4eda1a3cd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *************************************************************** | |
// MAP: Calculate the final grade for each student in the list below. | |
// Grade our students based on their performance on two projects (40% of final grade) | |
// and their final exam (60% of final grade). | |
// *************************************************************** | |
const students = [ | |
{ | |
name: 'Tony Parker', | |
firstProject: 80, | |
secondProject: 75, | |
finalExam: 90 | |
}, | |
{ | |
name: 'Marc Barchini', | |
firstProject: 84, | |
secondProject: 65, | |
finalExam: 65 | |
}, | |
{ | |
name: 'Claudia Lopez', | |
firstProject: 45, | |
secondProject: 95, | |
finalExam: 99 | |
} | |
]; | |
const finalList = students.map(student => { | |
// console.log(student); | |
const projAvg = (student.firstProject + student.secondProject) / 2; | |
const finalGrade = projAvg * 0.4 + student.finalExam * 0.6; | |
const updStudent = { | |
name: student.name, | |
finalGrade: Math.round(finalGrade) | |
}; | |
return updStudent; | |
}); | |
console.log(finalList); | |
// [ | |
// { name: 'Tony Parker', finalGrade: 85 }, | |
// { name: 'Marc Barchini', finalGrade: 69 }, | |
// { name: 'Claudia Lopez', finalGrade: 87 } | |
// ] | |
// REDUCE: Calculate the average rate | |
const product = { | |
name: 'AmazonBasics Apple Certified Lightning to USB Cable', | |
price: 7.99, | |
manufacter: 'Amazon', | |
reviews: [ | |
{ | |
user: 'Pavel Nedved', | |
comments: 'It was really usefull, strongly recommended', | |
rate: 4 | |
}, | |
{ user: 'Alvaro Trezeguet', comments: 'It lasted 2 days', rate: 1 }, | |
{ user: 'David Recoba', comments: 'Awesome', rate: 5 }, | |
{ user: 'Jose Romero', comments: 'Good value for money', rate: 4 }, | |
{ user: 'Antonio Cano', comments: 'It broked really fast', rate: 2 } | |
] | |
}; | |
const sumRate = product.reviews.reduce((acc, currValue) => acc + currValue.rate, 0); | |
const avgRate = sumRate / product.reviews.length; | |
console.log(avgRate); // => 3.2 | |
// FILTER | |
// Challenge 1: | |
// 1. In the given array, find the elements that start with "m" and make them capitalized. | |
// 2. Bonus: If the city name has two or more names, make them both capitalized (ex. Mexico City). | |
const cities1 = ["miami", "barcelona", "madrid", "amsterdam", "berlin", "sao paulo", "lisbon", "mexico city", "paris"]; | |
const capsMs = cities1.filter(theCity => theCity[0] === "m") | |
.map(city => { | |
if (city.includes(" ")){ | |
let splitted = city.split(" "); | |
const len = splitted.length; | |
for (let i = 0; i < len; i++){ | |
splitted[i] = splitted[i][0].toUpperCase() + splitted[i].slice(1); | |
} | |
return splitted.join(" "); | |
} | |
return city[0].toUpperCase() + city.slice(1) | |
}) | |
console.log(capsMs); | |
// SORT | |
// Sort the following array of objects by title value: [source-w3resource](https://www.w3resource.com/javascript-exercises/javascript-array-exercise-25.php) | |
const library = [ | |
{ author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254 }, | |
{ author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264 }, | |
{ | |
author: 'Suzanne Collins', | |
title: 'Mockingjay: The Final Book of The Hunger Games', | |
libraryID: 3245 | |
} | |
]; | |
function compareToSort(x, y) { | |
if (x.title < y.title) return -1; | |
if (x.title > y.title) return 1; | |
return 0; | |
} | |
console.log(library.slice().sort(compareToSort)); | |
// [ { author: 'Suzanne Collins', | |
// title: 'Mockingjay: The Final Book of The Hunger Games', | |
// libraryID: 3245 }, | |
// { author: 'Bill Gates', | |
// title: 'The Road Ahead', | |
// libraryID: 1254 }, | |
// { author: 'Steve Jobs', | |
// title: 'Walter Isaacson', | |
// libraryID: 4264 } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment