From d9b077c0ff8251490ca0b9e3c31eba9c26014fb4 Mon Sep 17 00:00:00 2001 From: Chrys-Nicolaides Date: Mon, 12 Oct 2020 15:38:59 -0700 Subject: [PATCH] Exercise: Level 3 --- solutions/day-01/main.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/solutions/day-01/main.js b/solutions/day-01/main.js index 7a4395a..6fa429c 100644 --- a/solutions/day-01/main.js +++ b/solutions/day-01/main.js @@ -46,3 +46,41 @@ const backEnd = ["Node", "Express", "MongoDB"]; const fullStack = [frontEnd, backEnd]; console.log(fullStack); + +// Exercise - Level 3 + +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]; +console.log(ages.sort()); + +const sum = ages.reduce((total, x) => total + x); +const len = ages.length; +console.log(sum / len); // average + +const mid = Math.ceil(len / 2); + +const median = len % 2 == 0 ? (ages[mid] + ages[mid - 1]) / 2 : ages[mid - 1]; + +console.log(median); // median + +const minIndex = ages[0]; +const maxIndex = ages[9]; + +console.log(maxIndex - minIndex); + +console.log(Math.abs(minIndex - sum / len)); +console.log(Math.abs(maxIndex - sum / len)); + +console.log(countries.slice(0, 10)); + +countries.pop(); + +if (countries.length % 2 != 0) { + console.log("odd"); + countries.push("Sweden"); + console.log(countries.slice(0, countries.length / 2)); + console.log(countries.slice(countries.length / 2)); +} else { + console.log("even"); + console.log(countries.slice(0, countries.length / 2)); + console.log(countries.slice(countries.length / 2)); +}