From f1e095555ccc22b6f0fc08b949b97e028ec40ebd Mon Sep 17 00:00:00 2001 From: Hussein Bahdon <35459369+H-BAHDON@users.noreply.github.com> Date: Tue, 25 Apr 2023 13:09:58 -0700 Subject: [PATCH] London10-Hussein-Bahdon-JS1-Week 4 --- 2-mandatory/1-create-functions.js | 24 ++++++++++++++---- 2-mandatory/2-oxygen-levels.js | 23 ++++++++++++++++- 2-mandatory/3-bush-berries.js | 10 +++++++- 2-mandatory/4-space-colonies.js | 15 +++++++++--- 2-mandatory/5-eligible-students.js | 10 +++++++- 2-mandatory/6-journey-planner.js | 33 ++++++++++++++++++++----- 2-mandatory/7-lane-names.js | 5 ++-- 2-mandatory/8-password-validator.js | 38 ++++++++++++++++++++++++++++- 8 files changed, 137 insertions(+), 21 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..da6446fa 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,8 @@ 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(arr) { + return arr.slice(0,5) } /* @@ -11,7 +12,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(arr ) { + return arr.slice( ).sort( ) } /* @@ -24,7 +26,10 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(arr) { + return arr.map(string =>{ + return string.trim().replace(/\//g, '').toLowerCase() + }) } /* @@ -33,7 +38,10 @@ Write a function that: - Returns a new array containing the same elements, but without the element at the passed index. */ -function remove() { +function remove(arr, index) { + let newArr = arr.slice() + newArr.splice(index, 1) + return newArr; } /* @@ -44,7 +52,13 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(arr) { + + return arr.map(value =>{ + let min = Math.min(value, 100); + let rounded = Math.round(100 * min) / 100; + return `${rounded}%`; + }) } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..a175edb2 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,28 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} + + + + +function findSafeOxygenLevel(oxygenLevels) { + + const safeLevels = oxygenLevels + .filter(index => { + return index.endsWith("%") + }) + .find(index => { + index = parseFloat(index.replace("%", "")) + const lower = 19.5; + const highest = 23.5; + return lower < index && index < highest; + }) + + return safeLevels; + + + +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..c62c51dd 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,9 +22,17 @@ */ function isBushSafe(berryArray) { - //Write your code here + const safeToEat = "Bush is safe to eat from"; + const notSafeEat = "Toxic! Leave bush alone!"; + + const isSafeToEat = berryArray.every((index) => { + return index === "pink"; + }); + + return isSafeToEat ? safeToEat : notSafeEat; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("isBushSafe finds toxic busy", () => { diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..aa301853 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -1,21 +1,28 @@ /* - The voyagers decide that they quite like this planet, and some of them want to settle there and colonise it. + The voyagers decide that they quite like this planet, and some of them want to settle + there and colonise it. - They call the planet "Alpha" and they decide that the FAMILIES whose last names start with 'A' should stay, + They call the planet "Alpha" and they decide that the FAMILIES whose last names start with + 'A' should stay, while the others go on in search of other planets to call home. Create a function that returns an array of colonisers that will stay, according to the above rules. NOTE: don't include any element that is not a "family". - HINT: Whenever you read the above the instructions, try to come up with the main input and output and logic + HINT: Whenever you read the above the instructions, try to come up with the main input + and output and logic Input: Is an array Output: Is an array Logic: Only strings that start with A, and finish with family */ -function getSettlers() {} +function getSettlers(family) { + return family.filter(i => { + return i.startsWith("A") && i.endsWith(" family") + }) +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..37c7af7c 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,15 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(students) { + const eligibleStduent = students.filter(index =>{ + const attendanceCount = index[1] + return attendanceCount >= 8; + }) + const names = eligibleStduent.map(index => index[0]) + return names; + +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..9fffd1a4 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -20,13 +20,15 @@ function checkCodeIsThere(stringText) { let magicWord = "code"; //edit code below - if (stringText) { - return stringText; + const containsCode = stringText.includes(magicWord); + if (containsCode) { + return stringText.indexOf(magicWord); } else { return "Not found"; } } + /* I am new to London and would like to know what transport I can take to different famous locations. The input provided contains a list of locations in London. Each of locations is followed by a list @@ -64,7 +66,11 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} + +function getTransportModes(locationAndModes) { + return locationAndModes.slice(1); +} + /* Implement the function isAccessibleByTransportMode that @@ -81,7 +87,11 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(transportModes, mode) { + return transportModes.includes(mode); +} + + /* Implement the function getLocationName that @@ -92,7 +102,10 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(locationArray) { + return locationArray[0]; +} + /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -121,8 +134,16 @@ function getLocationName() {} Advanced challange: try to use arrow function when invoking an array method. */ + + function journeyPlanner(locations, transportMode) { - // Implement the function body + return locations + .filter((location) => { + return isAccessibleByTransportMode(location, transportMode); + }) + .map((accessibleLocation) => { + return getLocationName(accessibleLocation); + }); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..a71e4760 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -5,8 +5,9 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ - -function getLanes() {} +function getLanes(streetNames) { + return streetNames.filter(i => i.indexOf('Lane') !== -1); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..e4cc1a39 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,43 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + const passwordSet = new Set(); + return passwords.map(password => { + if (password.length < 5) { + return false; + } + let hasUppercaseLetter = false; + let hasLowercaseLetter = false; + let hasNumber = false; + let hasSymbol = false; + for (let i = 0; i < password.length; i++) { + const char = password.charAt(i); + switch (true) { + case /[A-Z]/.test(char): + hasUppercaseLetter = true; + break; + case /[a-z]/.test(char): + hasLowercaseLetter = true; + break; + case /[0-9]/.test(char): + hasNumber = true; + break; + case /[!#$%.*&]/.test(char): + hasSymbol = true; + break; + } + } + if (!hasUppercaseLetter || !hasLowercaseLetter || !hasNumber || !hasSymbol) { + return false; + } + if (passwordSet.has(password)) { + return false; + } + passwordSet.add(password); + return true; + }); +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) {