Day 7 — Arrays — basics and iteration
Homework
Problem 1 Part 1
const names = ["xyz", "abc", "kal"];
const greetings = names.map(name => `Hello, ${name}`);
console.log(greetings);
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 2 Part 2
const nums = [1, 2, 3, 4, 5, 6, 7, 8];
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 3 Part 3
const evenSum = nums
.filter(n => n % 2 === 0)
.reduce((sum, n) => sum + n, 0);
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 4 Part 4
console.log(evenSum);
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 5 Part 5
const arr = [3, 1, 4, 1, 5, 9, 2, 6];
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 6 Part 6
const max1 = Math.max(...arr);
const max2 = arr.reduce((max, n) => n > max ? n : max, arr[0]);
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 7 Part 7
console.log(max1);
console.log(max2);
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 8 Part 8
function average(arr) {
const sum = arr.reduce((total, n) => total + n, 0);
return sum / arr.length ;
}
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).
Problem 9 Part 9
console.log(average([10, 20, 30])); // 20
Reference solution lives in ../js/homework.js (loaded at the bottom of this page).