From acea9d0a7f48db652d344fb90934664387823ee4 Mon Sep 17 00:00:00 2001 From: Yuliya110692 <122671093+Yuliya110692@users.noreply.github.com> Date: Sat, 29 Apr 2023 13:18:56 +0300 Subject: [PATCH 1/2] London Class 10- Yuliya Hospodar- JavaScript-Core-1-Coursework-Week4 --- 2-mandatory/1-create-functions.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..a1d09936 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); } /* From 640e43b6bf9853dad2e7eef784ad0e9fd9d3b92d Mon Sep 17 00:00:00 2001 From: Yuliya110692 <122671093+Yuliya110692@users.noreply.github.com> Date: Fri, 2 Jun 2023 09:49:58 +0100 Subject: [PATCH 2/2] London10 - Yulia - Javascript - core 1 - week 4 --- 2-mandatory/1-create-functions.js | 25 +++++++++++++----- 2-mandatory/2-oxygen-levels.js | 16 +++++++++-- 2-mandatory/3-bush-berries.js | 12 +++++++-- 2-mandatory/4-space-colonies.js | 19 ++++++++----- 2-mandatory/5-eligible-students.js | 14 ++++++---- 2-mandatory/6-journey-planner.js | 35 +++++++++++++++++++----- 2-mandatory/7-lane-names.js | 7 ++--- 2-mandatory/8-password-validator.js | 41 ++++++++++++++++++++++++++--- 8 files changed, 134 insertions(+), 35 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index a1d09936..402ba30c 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -4,7 +4,7 @@ Write a function that: - Returns a new array containing the first five elements of the passed array. */ function first5(arr) { - return arr.slice(0, 5); + return arr.slice(0,5) } /* @@ -12,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( ) } /* @@ -25,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() + }) } /* @@ -34,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; } /* @@ -45,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 ===== */ @@ -143,4 +156,4 @@ test("formatPercentage function works", () => { "100%", "0.37%", ]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..e36fc273 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,19 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(oxygenLevels) { + const safeLevel = 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 safeLevel; +} /* ======= TESTS - DO NOT MODIFY ===== */ @@ -35,4 +47,4 @@ test("findSafeOxygenLevel function filters out invalid percentages", () => { test("findSafeOxygenLevel function returns undefined if no valid planets found", () => { expect(findSafeOxygenLevel(["50"])).toBeUndefined(); -}); +}); \ No newline at end of file diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..1909af04 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 safe = "Bush is safe to eat from"; + const notSafe = "Toxic! Leave bush alone!"; + + const isSafe = berryArray.every((index) => { + return index === "pink"; + }); + + return isSafe ? safe : notSafe; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("isBushSafe finds toxic busy", () => { @@ -37,4 +45,4 @@ test("isBushSafe function finds safe bush", () => { expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual( "Bush is safe to eat from" ); -}); +}); \ No newline at end of file diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..482c18ca 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -1,22 +1,27 @@ /* - 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 ===== */ test("getSettlers function works", () => { @@ -43,4 +48,4 @@ test("getSettlers function works", () => { "Archer family", "A Great family", ]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..9d17c12e 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -6,11 +6,15 @@ (see tests to confirm how this data will be structured) - 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 ===== */ - test("getEligibleStudents function works", () => { const attendance = [ ["Ahmed", 8], @@ -31,4 +35,4 @@ test("getEligibleStudents function works", () => { test("getEligibleStudents function can return empty array", () => { const attendance = [["Jacob", 7]]; expect(getEligibleStudents(attendance)).toEqual([]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..1d313e3d 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 ===== */ @@ -209,4 +230,4 @@ describe("journeyPlanner", () => { "Tower Bridge", ]); }); -}); +}); \ No newline at end of file diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..fd868364 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 ===== */ @@ -20,4 +21,4 @@ test("getLanes function works", () => { ]; expect(getLanes(streetNames)).toEqual(["Abchurch Lane", "Addle Lane"]); -}); +}); \ No newline at end of file diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..c53b737e 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -22,8 +22,43 @@ Expected Result: 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) { @@ -69,4 +104,4 @@ test("Example 2", () => { "Pl3nty!", ]) ).toEqual([true, true, false, false, false]); -}); +}); \ No newline at end of file