From 7cf20c4c81236c8078d8110a966d8dea4697c308 Mon Sep 17 00:00:00 2001 From: siveromar <109865820+siveromar@users.noreply.github.com> Date: Sat, 25 Mar 2023 02:20:34 +0000 Subject: [PATCH 1/2] week3 --- 1-exercises/A-array-find/exercise.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..9ae4de60 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -4,7 +4,6 @@ */ // write your code here - let names = [ "Rakesh", "Antonio", @@ -16,10 +15,16 @@ let names = [ "Karim", "Ahmed", ]; +// === : +function findLongNameThatStartsWithA(array){ + const findedWords = array.find(word => word.length>7 && word[0]==='A') + return findedWords; +} -let longNameThatStartsWithA = findLongNameThatStartsWithA(names); +// let longNameThatStartsWithA = findLongNameThatStartsWithA(names); +// console.log(longNameThatStartsWithA); -console.log(longNameThatStartsWithA); +console.log(findLongNameThatStartsWithA(names)); /* EXPECTED OUTPUT */ // "Alexandra" From 8504c157a30886d659897d78aea6e0cec38be8bf Mon Sep 17 00:00:00 2001 From: siveromar <109865820+siveromar@users.noreply.github.com> Date: Tue, 4 Apr 2023 02:53:35 +0100 Subject: [PATCH 2/2] js.week4 --- 1-exercises/A-array-find/exercise.js | 4 +-- 1-exercises/B-array-some/exercise.js | 12 ++++++++ 1-exercises/C-array-every/exercise.js | 7 ++++- 1-exercises/D-array-filter/exercise.js | 3 +- 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 | 6 +++- 1-exercises/J-string-substring/exercise.js | 2 +- 1-exercises/J-string-substring/exercise2.js | 10 +++--- 1-exercises/J-string-substring/exercise3.js | 4 +-- 2-mandatory/1-create-functions.js | 34 +++++++++++++++++---- 2-mandatory/2-oxygen-levels.js | 18 +++++++++-- 2-mandatory/3-bush-berries.js | 7 +++++ 2-mandatory/4-space-colonies.js | 5 +-- 2-mandatory/5-eligible-students.js | 5 ++- 2-mandatory/6-journey-planner.js | 29 ++++++++++-------- 2-mandatory/7-lane-names.js | 6 ++-- 2-mandatory/8-password-validator.js | 16 +++++++++- 23 files changed, 149 insertions(+), 49 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 9ae4de60..c4d7cad8 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -17,8 +17,8 @@ let names = [ ]; // === : function findLongNameThatStartsWithA(array){ - const findedWords = array.find(word => word.length>7 && word[0]==='A') - return findedWords; + const findWords = array.find(word => word.length>7 && word[0]==='A') + return findWords; } // let longNameThatStartsWithA = findLongNameThatStartsWithA(names); diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..6294eee4 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -15,6 +15,13 @@ let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; +function program(number){ +if (number===null){ + process.exit(1); +} +} +pairsByIndex.some(program); + let pairs = pairsByIndex.map(function (indexes) { let student = students[indexes[0]]; let mentor = mentors[indexes[1]]; @@ -22,3 +29,8 @@ let pairs = pairsByIndex.map(function (indexes) { }); console.log(pairs); + + + + + diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..900911e9 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,12 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement + function checkGroup(groups){ + return groups.includes(students); + +} +let groupIsOnlyStudents= group.every(checkGroup); +// complete this statement 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..1cc6edac 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,7 +8,8 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement +let pairsByIndex=pairsByIndexRaw.filter(item => + Array.isArray(item) && item.length===2); // Complete this statement let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..d5248386 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,8 +3,8 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement - + // complete this statement +let numbersMultipliedByOneHundred = numbers.map(number => number *100); console.log(numbersMultipliedByOneHundred); /* EXPECTED RESULT diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..c7b55ec4 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,16 @@ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +arr.forEach(numbers => { + if(numbers % 3 === 0 && numbers % 5 === 0){ + console.log("FizzBuzz");} + else if(numbers % 3 === 0){ + console.log("Fizz");} + else if(numbers % 5 === 0){ + console.log("Buzz");} + else{ + console.log(numbers); + }}); /* EXPECTED OUTPUT */ /* diff --git a/1-exercises/G-array-methods/exercise.js b/1-exercises/G-array-methods/exercise.js index 4367ef6e..b9b5621f 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();// complete this statement /* 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..5f1e674a 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) // complete this statement /* 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..fdee733e 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(everyone.length - 5, everyone.length); /* 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..593187ca 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 splittedArr = str.split(""); + splittedArr[0] = splittedArr[0].toUpperCase(); + return splittedArr.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..06f53873 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..f8702c38 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,11 @@ 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; + result = result.replace(/dogs/gi, "cats"); + result = result.replace(/day/gi, "night"); + result = result.replace(/10/gi, "100000"); + result = result.replace(/great/gi, "brilliant"); /* EXPECTED OUTPUT */ diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js index 4624db68..35c09931 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,18); 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..6efbf5e8 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -8,8 +8,8 @@ let statement = "I do not like programming"; -let result = ""; - +let result = +statement.substring(0, 4) + statement.substring(8, statement.length); console.log(result); /* EXPECTED OUTPUT diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..c9165efb 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(arr) { + let newArr =arr.slice(0,5); + return newArr;} } /* @@ -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(arr) { + return arr.slice().sort(); } /* @@ -24,7 +27,14 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(arr) { + newArr = arr.map(function (string) { + string = string.trim(); + string = string.replace("/", ""); + string = string.toLowerCase(); + return string; + }); + return newArr; } /* @@ -33,9 +43,15 @@ 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 = []; + for (let i = 0; i < arr.length; i++) { + if (i != index) { + newArr.push(arr[i]); + } + } + return newArr; } - /* Write a function that: - Takes an array of numbers as input. @@ -44,7 +60,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) { + return "100%"; + } + return `${Math.round(number * 100) / 100}%` + }); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..5a2e9c63 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -10,9 +10,21 @@ Some string methods that might help you here are .replace() and .substring(). */ - -function findSafeOxygenLevel() {} - +function findSafeOxygenLevel(oxygenLevels) { + let firstProperPlanet = "" ; + for (const oxygenlevel of oxygenLevels) { + if(oxygenlevel.includes('%')) { + const oxygenlevelNum = Number(oxygenlevel.replace('%', '')); + if (oxygenlevelNum > 19.5 && oxygenlevelNum < 23.5){ + firstProperPlanet = oxygenlevel; + break + }} else { + continue + } + } + let output = firstProperPlanet === "" ? undefined : firstProperPlanet; + return output; +} /* ======= 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..90dee071 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -23,6 +23,13 @@ function isBushSafe(berryArray) { //Write your code here + let safeBush = berryArray.every(berry => berry.includes("pink")) + if(safeBush){ + 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..3e0b48b0 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,8 +15,9 @@ */ -function getSettlers() {} - +function getSettlers(allPeople) { + return allPeople.filter(person => person.includes('family') && person.startsWith('A')); +} /* ======= 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..21d96ebc 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,10 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(students) { + const eligibleStudents= students.filter(attendance => attendance[1]>=8) + return eligibleStudents.map(names=> names[0]); +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..6cd7b1a7 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -20,12 +20,11 @@ function checkCodeIsThere(stringText) { let magicWord = "code"; //edit code below - if (stringText) { - return stringText; - } else { - return "Not found"; - } -} + if (stringText.includes(magicWord)) { + 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. @@ -64,8 +63,9 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} - +function getTransportModes(londonLocations) { + return londonLocations.slice(1) +} /* Implement the function isAccessibleByTransportMode that @@ -81,7 +81,9 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(transportModes, transport) { + return transportModes.includes(transport); +} /* Implement the function getLocationName that @@ -92,8 +94,9 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} - +function getLocationName(locations) { + return locations.at(0) +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -122,8 +125,8 @@ function getLocationName() {} Advanced challange: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { - // Implement the function body -} + const accessibleLocations= locations.filter( el=> el.includes(transportMode)) +return accessibleLocations.map(getLocationName)} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..4499559d 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,8 +6,10 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} - +function getLanes(streets) { + return streets.filter(street => + street.includes("Lane")) +} /* ======= TESTS - DO NOT MODIFY ===== */ test("getLanes function works", () => { diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..e3b731bf 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,17 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + return passwords.map( + (el,index) => + containsLowercaseLetter(el) && + containsUppercaseLetter(el) && + containsNumber(el) && + containsSymbol(el) && + atLeastFiveCharacter(el) && + passwords.indexOf(el) === index + ); + } // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) { @@ -44,9 +54,13 @@ function containsNumber(string) { function containsSymbol(string) { return /[!#$%.*&]/.test(string); } +function atLeastFiveCharacter(string){ + return string.length>=5 +} /* ======= TESTS - DO NOT MODIFY ===== */ + test("Example 1", () => { expect( validatePasswords([