From 41f707f818b1d91bc9a7851ff22d160deb0316b1 Mon Sep 17 00:00:00 2001 From: onurat <114289826+onurat@users.noreply.github.com> Date: Mon, 15 May 2023 20:10:27 +0100 Subject: [PATCH] js 1 week 4 --- 2-mandatory/1-create-functions.js | 29 ++++++++++++++++++++++++----- 2-mandatory/2-oxygen-levels.js | 15 ++++++++++++++- 2-mandatory/3-bush-berries.js | 9 ++++++++- 2-mandatory/4-space-colonies.js | 7 ++++++- 2-mandatory/5-eligible-students.js | 9 ++++++++- 2-mandatory/7-lane-names.js | 5 ++++- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..ba437cc6 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,9 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the first five elements of the passed array. */ -function first5() { +function first5(array) { +return array.slice(0,5) + } /* @@ -11,7 +13,8 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the same elements, except sorted. */ -function sortArray() { +function sortArray(array) { +array.sort(); } /* @@ -24,7 +27,9 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(array) { + return array.map((array => array.trim().replace (/, ' '))); + } /* @@ -33,9 +38,14 @@ Write a function that: - Returns a new array containing the same elements, but without the element at the passed index. */ -function remove() { +function remove(array , index) { + return array.filter(function(element, idx) { + return idx !== index; + }); } + + /* Write a function that: - Takes an array of numbers as input. @@ -44,9 +54,18 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(numbers) { + return numbers.map(function(number) { + + var roundedNumber = Math.round(number * 100) / 100; + var cappedNumber = Math.min(roundedNumber, 100); + var formattedPercentage = cappedNumber.toFixed(2) + "%"; + + return formattedPercentage; +}); } + /* ======= TESTS - DO NOT MODIFY ===== */ test("first5 function works for more than five elements", () => { diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..22c99632 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,20 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(planets) { + + for (let i = 0; i < planets.length; i++) { + + const planet = planets[i]; + const oxygenLevel = Number(planet.substring(0, planet.length - 1)); + if (oxygenLevel >= 19.5 && oxygenLevel <= 23.5) { + return planet; + } + } + return "No safe oxygen level found."; + } + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..42a6c824 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,7 +22,14 @@ */ function isBushSafe(berryArray) { - //Write your code here + const hasNonPinkBerries = berryArray.some(berry => berry !== 'pink'); + const hasOnlyPinkBerries = !hasNonPinkBerries && berryArray.length > 0; + + if (hasOnlyPinkBerries) { + return "bush is safe to eat from."; + } else { + return "Toxic! Leave bush alone!"; + } } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..918e06b3 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,12 @@ */ -function getSettlers() {} +function getSettlers(colonisers) +{ const settlers = colonisers.filter(coloniser => { + return coloniser.startsWith('A') && coloniser.endsWith('family'); +}); + +return settlers;} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..2a75f360 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,14 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents() { + const eligibleStudents = students + .filter(student => student.attendance >= 8) + .map(student => student.name); + + return eligibleStudents; + +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..9782ef4d 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,7 +6,10 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +function getLanes(streetNames) { + return streetNames.filter(name => name.includes('Lane')); + } + /* ======= TESTS - DO NOT MODIFY ===== */