From 4afd43fcba1a9faf546f890c2785a1ed28ce0dde Mon Sep 17 00:00:00 2001 From: Shadi38 <113238379+Shadi38@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:46:13 +0000 Subject: [PATCH] finishing exercises and mandatory --- 1-exercises/A-array-find/exercise.js | 16 +++++- 1-exercises/B-array-some/exercise.js | 11 ++++ 1-exercises/C-array-every/exercise.js | 6 ++- 1-exercises/D-array-filter/exercise.js | 8 ++- 1-exercises/E-array-map/exercise.js | 7 ++- 1-exercises/F-array-forEach/exercise.js | 19 +++++++ 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 | 8 ++- 1-exercises/H-array-methods-2/exercise3.js | 2 +- 1-exercises/I-string-replace/exercise.js | 5 +- 1-exercises/J-string-substring/exercise2.js | 16 ++++-- 2-mandatory/1-create-functions.js | 57 ++++++++++++++++++--- 2-mandatory/2-oxygen-levels.js | 17 +++++- 2-mandatory/3-bush-berries.js | 8 ++- 2-mandatory/4-space-colonies.js | 11 +++- 2-mandatory/5-eligible-students.js | 11 +++- 2-mandatory/6-journey-planner.js | 37 ++++++++++--- 2-mandatory/7-lane-names.js | 6 ++- 2-mandatory/8-password-validator.js | 26 +++++++++- 21 files changed, 241 insertions(+), 38 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..be515656 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -2,8 +2,16 @@ You are given an array of names. Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters. */ +function findLongNameThatStartsWithA(names){ + let startA = names.charAt(0); + let longname = names.length; + if(startA === "A" && longname > 7 ){ + return names; + } + +} + -// write your code here let names = [ "Rakesh", @@ -17,7 +25,11 @@ let names = [ "Ahmed", ]; -let longNameThatStartsWithA = findLongNameThatStartsWithA(names); +//let longNameThatStartsWithA =findLongNameThatStartsWithA(names); + +let longNameThatStartsWithA = names.find(findLongNameThatStartsWithA); + + console.log(longNameThatStartsWithA); diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..1dcd1cf8 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -22,3 +22,14 @@ let pairs = pairsByIndex.map(function (indexes) { }); console.log(pairs); + + function checkArray(check){ +if(check===null){ + process.exit(1); + return "error" +} + +} + +let finalcheck = pairsByIndex.some(checkArray); +console.log(finalcheck); \ No newline at end of file diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..c5ebf440 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -2,10 +2,14 @@ This program should check if the array `group` contains only students */ +function check(student){ +return students.includes[student]; +} + let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +let groupIsOnlyStudents = group.every(check); 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..11ad5740 100644 --- a/1-exercises/D-array-filter/exercise.js +++ b/1-exercises/D-array-filter/exercise.js @@ -8,11 +8,13 @@ let pairsByIndexRaw = [[0, 3], [1, 2], [2, 1], null, [1], false, "whoops"]; -let pairsByIndex; // Complete this statement + let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; +let pairsByIndex = pairsByIndexRaw.filter(item => Array.isArray(item) && item.length === 2); + let pairs = pairsByIndex.map(function (indexes) { let student = students[indexes[0]]; let mentor = mentors[indexes[1]]; @@ -21,6 +23,10 @@ let pairs = pairsByIndex.map(function (indexes) { console.log(pairs); + + +console.log(pairsByIndex); + /* EXPECTED RESULT [ [ 'Islam', 'Luke' ], [ 'Lesley', 'Mozafar' ], [ 'Harun', 'Irina' ] ] diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..245c6875 100644 --- a/1-exercises/E-array-map/exercise.js +++ b/1-exercises/E-array-map/exercise.js @@ -3,10 +3,15 @@ let numbers = [0.1, 0.2, 0.3, 0.4, 0.5]; -let numbersMultipliedByOneHundred; // complete this statement +let numbersMultipliedByOneHundred = numbers.map(function multyple(number){ +return number*100; +}) + +let numbersMultipliedByOneHundred = numbers.map(number => number * 100); console.log(numbersMultipliedByOneHundred); + /* EXPECTED RESULT [10, 20, 30, 40, 50] diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..f035295d 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -7,8 +7,27 @@ An array with numbers 1-15 has been provided. */ + let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + +function test(number){ + + if (number % 3 == 0 && number % 5 == 0){ + console.log("FizzBuzz"); + }else if (number % 3 == 0){ + + console.log("Fizz"); + + }else if (number % 5 == 0){ + console.log('Buzz'); + } +console.log(number); +} + + +arr.forEach(test); + /* 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..f43b693c 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(2) /* 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..2c67f5c1 100644 --- a/1-exercises/H-array-methods-2/exercise2.js +++ b/1-exercises/H-array-methods-2/exercise2.js @@ -7,7 +7,13 @@ Tip: use the string method .split() and the array method .join() */ -function capitalise(str) {} +function capitalise(str) { +let change = str.split(""); +change[0] = change[0].toUpperCase(); +let final = change.join(""); +return final; +} + /* 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..1c8ea38c 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,10 @@ 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", "cats"); +let result2 = result.replace("day", "night"); +let result3 = result2.replace(10, 1000000); +let result4 = result3.replace("great day", "brilliant night"); /* EXPECTED OUTPUT */ diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..251f5e73 100644 --- a/1-exercises/J-string-substring/exercise2.js +++ b/1-exercises/J-string-substring/exercise2.js @@ -6,6 +6,12 @@ For example, if the name is "John Smith" you should return "John" */ +function firstName(names){ + +} + + + let names = [ "Tamzin Lindsay", "Jessica Tate", @@ -14,11 +20,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/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..1cc8161e 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,17 +3,37 @@ 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; } + + + + + + + /* 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(); + } + + + + + + + /* 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. @@ -24,16 +44,30 @@ 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 change(string) { + string = string.trim(); + string = string.replace("/", ""); + string = string.toLowerCase(); + return string; + }); + return newarr; } - /* 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(arr, index) { + let result = []; + for (let i = 0; i < arr.length; i++) { + if (i !== index) { + result.push(arr[i]); + } + } + return result; } /* @@ -44,9 +78,18 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { -} +function formatPercentage(numbers) { + let newnumbers = numbers.map(function (number) { + if (number > 100) { + number = "100%"; + } else { + number = `${Math.round(number * 100) / 100}%`; + } + return number; + }); + return newnumbers; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("first5 function works for more than five elements", () => { diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..d7024310 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,22 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(arr) { + let newArr = arr.filter(function (oxygen) { + let newoxygen = oxygen.substring(0, oxygen.length - 1); + let oxygennumber = parseFloat(newoxygen); + + if (oxygennumber > 0) { + if (!isNaN(oxygennumber) && oxygennumber > 19.5 && oxygennumber < 23.5) { + return oxygen; + } + } + }); + + let result = newArr[0]; + return result; +} + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..7e8331f2 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,7 +22,13 @@ */ function isBushSafe(berryArray) { - //Write your code here + let result = berryArray.every(berry => berry==="pink") + if (result){ + 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..f5359faa 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,8 +15,15 @@ */ -function getSettlers() {} - +function getSettlers(arr) { + let newarr = arr.filter(function (voyager) { + let last6letter = voyager.substring(voyager.length - 6, voyager.length); + if (voyager[0] === "A" && last6letter === "family") { + return voyager; + } + }); + return newarr; +} /* ======= 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..01d0e5cb 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,16 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} + function getEligibleStudents(arr) { + const newarr = []; + for (let name of arr) { + if (name[1] >= 8) { + newarr.push(name[0]); + } + + } + return newarr; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..e060bd16 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("code")) { + return stringText.indexOf("code"); } else { return "Not found"; } @@ -64,7 +64,15 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} +function getTransportModes(arr) { + const result = []; + let i=1; + while(i { + return isAccessibleByTransportMode(locationElement, transportMode); + }); + let locationNames = newlocation.map(function getLocationName( + locationElement + ) { + return locationElement[0]; + }); + return locationNames; } + + + + + /* ======= TESTS - DO NOT MODIFY ===== */ const string1 = "I Love coding and perfect code makes me happy"; diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..4b6a4f3d 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(streetNames) { + let resultNames = streetNames.filter(streetName => streetName.includes('Lane')); + return resultNames; +} /* ======= 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..1de6b486 100644 --- a/2-mandatory/8-password-validator.js +++ b/2-mandatory/8-password-validator.js @@ -23,7 +23,31 @@ PasswordValidationResult= [false, false, false, false, true] */ -function validatePasswords(passwords) {} +function validatePasswords(passwords) { + let previousPasswords = []; + return passwords.map((password) => { + if (password.length < 5) { + return false; + } + if (!containsUppercaseLetter(password)) { + return false; + } + if (!containsLowercaseLetter(password)) { + return false; + } + if (!containsNumber(password)) { + return false; + } + if (!containsSymbol(password)) { + return false; + } + if (previousPasswords.includes(password)) { + return false; + } + previousPasswords.push(password); + return true; + }); +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) {