From b818d949d4e375512a612f6402fef787419a93ec Mon Sep 17 00:00:00 2001 From: Andrius Isin Date: Sat, 11 Mar 2023 16:44:40 +0000 Subject: [PATCH 1/4] changed --- 1-exercises/A-array-find/exercise.js | 9 +++++++-- 1-exercises/B-array-some/exercise.js | 10 +++++++--- 1-exercises/C-array-every/exercise.js | 8 +++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..2f9b3c09 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -16,8 +16,13 @@ let names = [ "Karim", "Ahmed", ]; - -let longNameThatStartsWithA = findLongNameThatStartsWithA(names); +function getlongNameThatStartsWithA(name) { + return name > 7 && name.startsWith("A"); +} +let longNameThatStartsWithA = names.find(getlongNameThatStartsWithA); +// let longNameThatStartsWithA = names.find((element) => { +// return element.length > 7 && element.startsWith("A"); +// }); console.log(longNameThatStartsWithA); diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..19af6a97 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -16,9 +16,13 @@ let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; let pairs = pairsByIndex.map(function (indexes) { - let student = students[indexes[0]]; - let mentor = mentors[indexes[1]]; - return [student, mentor]; + if (indexes !== null) { + let student = students[indexes[0]]; + let mentor = mentors[indexes[1]]; + return [student, mentor]; + } else { + process.exit([1]); + } }); console.log(pairs); diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..fadd5caf 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,13 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +let groupIsOnlyStudents = group.map(function (student) { + if (student == null) { + return true; + } else { + return false; + } +}); if (groupIsOnlyStudents) { console.log("The group contains only students"); From bd44efc60ed08e0b0b94b92a3b370e4022c3dc76 Mon Sep 17 00:00:00 2001 From: AndriusIsin Date: Mon, 27 Mar 2023 18:52:40 +0100 Subject: [PATCH 2/4] done exercises --- 1-exercises/C-array-every/exercise.js | 10 ++-------- 1-exercises/D-array-filter/exercise.js | 6 ++++-- 1-exercises/E-array-map/exercise.js | 4 ++-- 1-exercises/F-array-forEach/exercise.js | 10 +++++++++- 1-exercises/G-array-methods/exercise.js | 2 +- 1-exercises/G-array-methods/exercise2.js | 2 +- 1-exercises/H-array-methods-2/exercise.js | 4 ++-- 1-exercises/H-array-methods-2/exercise2.js | 6 +++++- 1-exercises/H-array-methods-2/exercise3.js | 2 +- 1-exercises/I-string-replace/exercise.js | 7 ++++++- 1-exercises/J-string-substring/exercise.js | 2 +- 1-exercises/J-string-substring/exercise2.js | 10 +++++----- 1-exercises/J-string-substring/exercise3.js | 5 ++++- 13 files changed, 43 insertions(+), 27 deletions(-) diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index fadd5caf..eda572a1 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -3,15 +3,9 @@ */ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; -let group = ["Austine", "Dany", "Swathi", "Daniel"]; +let group = ["Austine", "Dany", "Swathi"]; -let groupIsOnlyStudents = group.map(function (student) { - if (student == null) { - return true; - } else { - return false; - } -}); +let groupIsOnlyStudents = group.every((element) => students.includes(element)); if (groupIsOnlyStudents) { console.log("The group contains only students"); diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js index 51837028..662655b0 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,9 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +let pairsByIndex = pairsByIndexRaw.filter((element) => { + return Array.isArray(element) && element.length === 2; +}); let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; @@ -24,4 +26,4 @@ console.log(pairs); /* EXPECTED RESULT [ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ] -*/ \ No newline at end of file +*/ diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..c9e8a976 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,11 +3,11 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +let numbersMultipliedByOneHundred = numbers.map((num) => num * 100); console.log(numbersMultipliedByOneHundred); /* EXPECTED RESULT [10, 20, 30, 40, 50] -*/ \ No newline at end of file +*/ diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..c3912466 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -8,7 +8,15 @@ */ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; - +let newArray = arr.forEach((number) => { + if (number % 3 === 0) { + console.log("Fizz"); + } else if (number % 5 === 0) { + console.log("Buzz"); + } else { + console.log(number); + } +}); /* EXPECTED OUTPUT */ /* diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..e3be4894 100644 --- a/1-exercises/G-array-methods/exercise.js +++ b/1-exercises/G-array-methods/exercise.js @@ -4,7 +4,7 @@ */ let numbers = [3, 2, 1]; -let sortedNumbers; // complete this statement +let sortedNumbers = numbers.sort(); /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/G-array-methods/exercise2.js b/1-exercises/G-array-methods/exercise2.js index 4c68c3a6..68887268 100644 --- a/1-exercises/G-array-methods/exercise2.js +++ b/1-exercises/G-array-methods/exercise2.js @@ -7,7 +7,7 @@ let mentors = ["Daniel", "Irina", "Rares"]; let students = ["Rukmini", "Abdul", "Austine", "Swathi"]; -let everyone; // complete this statement +let everyone = mentors.concat(students); /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise.js b/1-exercises/H-array-methods-2/exercise.js index 59c5daa7..31edcd81 100644 --- a/1-exercises/H-array-methods-2/exercise.js +++ b/1-exercises/H-array-methods-2/exercise.js @@ -15,8 +15,8 @@ let everyone = [ "Swathi", ]; -let firstFive; // complete this statement -let lastFive; // complete this statement +let firstFive = everyone.slice(0, 5); +let lastFive = everyone.slice(-5); /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise2.js b/1-exercises/H-array-methods-2/exercise2.js index 14bb4318..cf882c15 100644 --- a/1-exercises/H-array-methods-2/exercise2.js +++ b/1-exercises/H-array-methods-2/exercise2.js @@ -7,7 +7,11 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { + let arr = str.split(""); + arr[0] = arr[0].toUpperCase(); + return arr.join(""); +} /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/H-array-methods-2/exercise3.js b/1-exercises/H-array-methods-2/exercise3.js index c8e079e4..8e5ec6a0 100644 --- a/1-exercises/H-array-methods-2/exercise3.js +++ b/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,7 @@ let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement + return ukNations.includes(country); } /* diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js index 3f7104d7..465a1515 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,12 @@ let story = "I like dogs. One day I went to the park and I saw 10 dogs. It was a great day."; -let result = story.replace("", ""); +let result = story + .replace(/dogs/g, "cats") + .replace(/day/g, "night") + .replace(/great/g, "brilliant") + .replace(/10/g, "100000"); +console.log(result); /* EXPECTED OUTPUT */ diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js index 4624db68..488d1f5d 100644 --- a/1-exercises/J-string-substring/exercise.js +++ b/1-exercises/J-string-substring/exercise.js @@ -6,7 +6,7 @@ let statement = "I like programming and dogs"; -statement = statement.substring(); +statement = statement.substring(0, 19); console.log(statement); diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..cb541c6a 100644 --- a/1-exercises/J-string-substring/exercise2.js +++ b/1-exercises/J-string-substring/exercise2.js @@ -14,11 +14,11 @@ let names = [ "Arron Graham", ]; -names[0] = names[0].substring(); -names[1] = names[1].substring(); -names[2] = names[2].substring(); -names[3] = names[3].substring(); -names[4] = names[4].substring(); +names[0] = names[0].substring(0, 6); +names[1] = names[1].substring(0, 7); +names[2] = names[2].substring(0, 4); +names[3] = names[3].substring(0, 4); +names[4] = names[4].substring(0, 5); names.forEach((name) => { console.log(name); diff --git a/1-exercises/J-string-substring/exercise3.js b/1-exercises/J-string-substring/exercise3.js index 14f77417..4fa3a6f1 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -8,7 +8,10 @@ let statement = "I do not like programming"; -let result = ""; +let result = `${statement.substring(0, 4)}${statement.substring( + 8, + statement.length +)}`; console.log(result); From 79ca8addb391e5e87e16382a39414a87ac03bb6e Mon Sep 17 00:00:00 2001 From: AndriusIsin Date: Wed, 29 Mar 2023 13:13:49 +0100 Subject: [PATCH 3/4] done mandatory --- 2-mandatory/1-create-functions.js | 26 ++++++++++++----- 2-mandatory/1.js | 43 +++++++++++++++++++++++++++++ 2-mandatory/2-oxygen-levels.js | 13 +++++++-- 2-mandatory/3-bush-berries.js | 7 ++++- 2-mandatory/4-space-colonies.js | 6 +++- 2-mandatory/5-eligible-students.js | 6 +++- 2-mandatory/6-journey-planner.js | 20 ++++++++++---- 2-mandatory/7-lane-names.js | 4 ++- 2-mandatory/8-password-validator.js | 17 +++++++++++- 9 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 2-mandatory/1.js diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..38a16dfd 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -2,8 +2,10 @@ Write a function that: - Accepts an array as a parameter. - Returns a new array containing the first five elements of the passed array. +npm test -- --testPathPattern 1-create-functions.js */ -function first5() { +function first5(array) { + return array.slice(0, 5); } /* @@ -11,12 +13,14 @@ 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) { + return array.slice().sort(); } /* -NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week. -Think about what is better about this solution than your one last week, and what is worse. +NOTE: This exercise is the same as one you did last week - try to do it again using things +you learnt this week. Think about what is better about this solution than your one last + week, and what is worse. Write a function that: - Takes an array of strings as input. @@ -24,7 +28,8 @@ 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().replace("/", "").toLowerCase()); } /* @@ -33,7 +38,8 @@ 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((element, i) => i !== index); } /* @@ -44,7 +50,13 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(numbers) { + return numbers.map((number) => { + if (number > 100) { + number = 100; + } + return `${Math.round(number * 100) / 100}%`; + }); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/1.js b/2-mandatory/1.js new file mode 100644 index 00000000..35789d9f --- /dev/null +++ b/2-mandatory/1.js @@ -0,0 +1,43 @@ +function validatePasswords(passwords) { + return passwords.map((password, index) => { + if ( + // index !== passwords.indexOf(password) || + password.length < 5 || + !containsUppercaseLetter(password) || + !containsLowercaseLetter(password) || + !containsNumber(password) || + !containsSymbol(password) + ) { + return false; + } else { + return true; + } + }); +} + +// 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); +} + +// Returns true if string contains at least one number. +function containsNumber(string) { + return /[0-9]/.test(string); +} + +// Returns true if string contains at least one symbol. +function containsSymbol(string) { + return /[!#$%.*&]/.test(string); +} + +/* ======= TESTS - DO NOT MODIFY ===== */ +let num1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"]; +let num2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"]; + +console.log(validatePasswords(num1)); +console.log(validatePasswords(num2)); diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..f4ac6765 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,8 +11,17 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} - +function findSafeOxygenLevel(oxygen) { + let firstSafePlanet = oxygen + .filter((planet) => planet.includes("%")) + .map((planet) => Number(planet.replace("%", ""))) + .find((planet) => planet > 19.5 && planet < 23.5); + if (firstSafePlanet === undefined) { + return firstSafePlanet; + } else { + return firstSafePlanet + "%"; + } +} /* ======= TESTS - DO NOT MODIFY ===== */ test("findSafeOxygenLevel function works - case 1", () => { diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..0dbbd64b 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,7 +22,12 @@ */ function isBushSafe(berryArray) { - //Write your code here + let safeToEat = berryArray.every((color) => color === "pink"); + if (safeToEat) { + 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..000ae5d6 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,11 @@ */ -function getSettlers() {} +function getSettlers(voyagers) { + return voyagers.filter( + (family) => family.startsWith("A") && family.includes("family") + ); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..90681d93 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,11 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(students) { + return students + .filter((student) => student[1] >= 8) + .map((student) => student[0]); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..1cbd60df 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.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(array) { + return array.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(modes, transportMode) { + return modes.includes(transportMode); +} /* 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(locationAndTransports) { + return locationAndTransports[0]; +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -122,7 +128,9 @@ 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) => location.includes(transportMode)) + .map((location) => location[0]); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..eb4bef6a 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.indexOf("Lane") > 0); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..effcd7de 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,22 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + return passwords.map((password, index) => { + if ( + index !== passwords.indexOf(password) || + password.length < 5 || + !containsUppercaseLetter(password) || + !containsLowercaseLetter(password) || + !containsNumber(password) || + !containsSymbol(password) + ) { + return false; + } else { + return true; + } + }); +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { From 49a2803ab0b9df4b45afd3fa14e70a5dc6da5b3f Mon Sep 17 00:00:00 2001 From: AndriusIsin Date: Wed, 29 Mar 2023 13:17:15 +0100 Subject: [PATCH 4/4] Delete 1.js --- 2-mandatory/1.js | 43 ------------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 2-mandatory/1.js diff --git a/2-mandatory/1.js b/2-mandatory/1.js deleted file mode 100644 index 35789d9f..00000000 --- a/2-mandatory/1.js +++ /dev/null @@ -1,43 +0,0 @@ -function validatePasswords(passwords) { - return passwords.map((password, index) => { - if ( - // index !== passwords.indexOf(password) || - password.length < 5 || - !containsUppercaseLetter(password) || - !containsLowercaseLetter(password) || - !containsNumber(password) || - !containsSymbol(password) - ) { - return false; - } else { - return true; - } - }); -} - -// 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); -} - -// Returns true if string contains at least one number. -function containsNumber(string) { - return /[0-9]/.test(string); -} - -// Returns true if string contains at least one symbol. -function containsSymbol(string) { - return /[!#$%.*&]/.test(string); -} - -/* ======= TESTS - DO NOT MODIFY ===== */ -let num1 = ["Se%5", "TktE.TJTU", "384#HsHF", "dvyyeyy!5", "tryT3729"]; -let num2 = ["StUFf27%", "Pl3nty!", "Jai33", "shajsaUA**&&", "Pl3nty!"]; - -console.log(validatePasswords(num1)); -console.log(validatePasswords(num2));