From 0e21b62228b1265ad11364070d8ee17c9a3aebd8 Mon Sep 17 00:00:00 2001 From: Ehdaa Munier <109866621+Ehdaa-Munier@users.noreply.github.com> Date: Sun, 9 Apr 2023 09:34:42 +0100 Subject: [PATCH 1/4] finish mandatory 3 --- .gitignore | 4 ++- 2-mandatory/1-create-functions.js | 38 +++++++++++++++++++++++++---- 2-mandatory/2-oxygen-levels.js | 13 +++++++++- 2-mandatory/3-bush-berries.js | 7 ++++++ 2-mandatory/8-password-validator.js | 37 ++++++++++++++++++++++++++-- 5 files changed, 90 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 2db748e3..a8ead4a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules .DS_Store -/package-lock.json \ No newline at end of file +/package-lock.json +practice.js +*.js diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..f50871a0 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,13 @@ 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) { +//let numbers = [ 1, 2, 3, 4, 5, 6, 7] +let copyOfOriginal = Arr.slice(0, 5); { + return copyOfOriginal; +} + + } /* @@ -11,7 +17,10 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the same elements, except sorted. */ -function sortArray() { +function sortArray(letters) { + let copyOfOriginal = [...letters]; + copyOfOriginal.sort(); + return copyOfOriginal; } /* @@ -24,7 +33,11 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(array) { + + + return array.map((string) => string.trim().toLowerCase().replace("/", "")); + } /* @@ -32,8 +45,13 @@ Write a function that: - Takes an array and an index as input. - Returns a new array containing the same elements, but without the element at the passed index. */ +// + + -function remove() { +function remove(array, index) { + +return array.slice(0, index).concat(array.slice(index + 1)); } /* @@ -44,7 +62,17 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(numbers) { + numbersWithPercentages = numbers.map(addPercentage); + return numbersWithPercentages; + + } + function addPercentage(number) { + if (number > 100) { + number = 100; + } + number = Math.round(number * 100) / 100; + return `${number}%`; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..0508802d 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,18 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(array) { + + array.find((element) => { + return ( + Number(element.replace("%", "")) > 19.5 && + Number(element.replace("%", "")) < 23.5 + ); + }); + +} + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..262c62e8 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -23,8 +23,15 @@ function isBushSafe(berryArray) { //Write your code here + + if (berryArray.every((berry) => berry === "pink")) { + return "Bush is safe to eat from"; + } else { + return "Toxic! Leave bush alone!"; + } } + /* ======= TESTS - DO NOT MODIFY ===== */ test("isBushSafe finds toxic busy", () => { diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..7cd3793d 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,15 +23,48 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} + +const arr = ["fhD8!yrjj", "ttkT", "dvyy", "ttkT", "qwbfj76%", "tytT3729."]; + +function validatePasswords(passwords) { + const result = []; + for (let i = 0; i < passwords.length; i++) { + const check1 = containsAtLeast5Chars(passwords[i]); + const check2 = containsUppercaseLetter(passwords[i]); + const check3 = containsLowercaseLetter(passwords[i]); + const check4 = containsNumber(passwords[i]); + const check5 = containsSymbol(passwords[i]); + let isNotRepeatedPassword = true; + for (let j = 0; j < i; j++) { + if (passwords[j] === passwords[i]) { + isNotRepeatedPassword = false; + } + } + result.push( + check1 && check2 && check3 && check4 && check5 && isNotRepeatedPassword + ); + } + return result; +} + + + + + + + +// Returns true if string has at least 5 characters +function containsAtLeast5Chars(string) { + return string.length >= 5; +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { return /[A-Z]/.test(string); } - // Returns true if string contains at least one lowercase letter. function containsLowercaseLetter(string) { + return /[a-z]/.test(string); } From b7f22a220d908f20fee530b81d54bdf73a7e0c54 Mon Sep 17 00:00:00 2001 From: Ehdaa Munier <109866621+Ehdaa-Munier@users.noreply.github.com> Date: Sun, 9 Apr 2023 09:36:15 +0100 Subject: [PATCH 2/4] finish mandatory 1 --- 2-mandatory/1-create-functions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index f50871a0..09977e09 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -40,6 +40,7 @@ function tidyUpString(array) { } + /* Write a function that: - Takes an array and an index as input. From 5fa894909fce3e0a0c1b7dad0012e445657494cb Mon Sep 17 00:00:00 2001 From: Ehdaa Munier <109866621+Ehdaa-Munier@users.noreply.github.com> Date: Sun, 9 Apr 2023 09:37:02 +0100 Subject: [PATCH 3/4] finish mandatory 8 --- 2-mandatory/8-password-validator.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index 7cd3793d..3dfdf6cb 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -50,9 +50,6 @@ function validatePasswords(passwords) { - - - // Returns true if string has at least 5 characters function containsAtLeast5Chars(string) { return string.length >= 5; From fd21dcec8b50adf3cb80cb0aad6200bee9ab47a7 Mon Sep 17 00:00:00 2001 From: Ehdaa Munier <109866621+Ehdaa-Munier@users.noreply.github.com> Date: Sun, 9 Apr 2023 15:01:00 +0100 Subject: [PATCH 4/4] mandatory 2, 4, 5, 6, 7 --- 2-mandatory/2-oxygen-levels.js | 16 +++++++--------- 2-mandatory/4-space-colonies.js | 11 ++++++++++- 2-mandatory/5-eligible-students.js | 6 +++++- 2-mandatory/6-journey-planner.js | 18 +++++++++++++----- 2-mandatory/7-lane-names.js | 4 +++- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 0508802d..becc8f3e 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -10,17 +10,15 @@ Some string methods that might help you here are .replace() and .substring(). */ +let oxegenLevel= []; +function findSafeOxygenLevel(result) { + if(value > 19.5 && value < 23.5) + return result === "%", " . "; +} -function findSafeOxygenLevel(array) { - - array.find((element) => { - return ( - Number(element.replace("%", "")) > 19.5 && - Number(element.replace("%", "")) < 23.5 - ); - }); +//let array=[value]; + //return array.find((value) => value > 19.5 , "%" && value < 23.5 , "%"); -} diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..830caaab 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,8 +15,17 @@ */ -function getSettlers() {} +function getSettlers(voyagers) { + let array=[]; + let array1= [ array+"family" , "family"]; + for(let familyName of voyagers){ + if(familyName.split(' ').slice(-1).join(' ') && familyName[0] === "A"){ + array.push(familyName)} +} + +return array1; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("getSettlers function works", () => { diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..651738fe 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,8 +7,12 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(attended) { + const eligibleAttendance = attended.filter(attendance => attendance[1] >= 8); + return eligibleAttendance.map(names=> names[0]) + +} /* ======= TESTS - DO NOT MODIFY ===== */ test("getEligibleStudents function works", () => { diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..483c08a0 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -20,8 +20,8 @@ function checkCodeIsThere(stringText) { let magicWord = "code"; //edit code below - if (stringText) { - return stringText; + if (stringText.text.includes(magicWord)) { + return stringText.indexOf(magicWord);; } else { return "Not found"; } @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} +function getTransportModes(location) { + return location.slice(1); +} /* Implement the function isAccessibleByTransportMode that @@ -81,7 +83,9 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(transportMode, transport) { + return transportMode.includes(transport); +} /* Implement the function getLocationName that @@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(Array) { + return Array[0]; +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -122,6 +128,8 @@ function getLocationName() {} Advanced challange: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { + return locations.filter((Array) => Arrayincludes(transportMode)) + .map((location) => Array[0]) // Implement the function body } diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..41b82244 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,7 +6,9 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +function getLanes(streetNames) { + return streetNames.filter((street) => street.includes("Lane")); +} /* ======= TESTS - DO NOT MODIFY ===== */