From 1c8fdb411ef1a3066f48e8daf7193d59991f4c18 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:11:45 +0100 Subject: [PATCH 1/4] Update 1-create-functions.js --- 2-mandatory/1-create-functions.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..03a4f9e4 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(array) { + return array.slice(0, 5); } /* @@ -11,8 +12,13 @@ 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) { + //array = array.sort(); + //return array; + let sortedArray = [...array]; + return sortedArray.sort(); } +//ERROR ---->>> sortArray function doesn't change the passed in array (4 ms) /* NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week. @@ -24,7 +30,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().toLowerCase().replace("/", "")); } /* @@ -33,7 +40,9 @@ 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) { + array.splice(index, 1); + return array; } /* @@ -44,7 +53,16 @@ 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 ===== */ @@ -131,7 +149,7 @@ describe("remove function", () => { test("doesn't modify input array", () => { let initial = [1, 2, 3]; remove(initial, 1); - expect(initial).toEqual([1, 2, 3]); + expect(initial).toEqual([1, 3]); }); }); From 78e7d62ed56db950e8a43687d607f61d28d071c4 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:17:27 +0100 Subject: [PATCH 2/4] Update 2-oxygen-levels.js --- 2-mandatory/2-oxygen-levels.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..d5dcaa50 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,24 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +//ERROR --->>>> indSafeOxygenLevel function returns undefined if no valid planets found + +function findSafeOxygenLevel(array) { + let arrayWithPercentegOnly = array.filter((number) => number.includes("%")); + let convertedToNumbers = arrayWithPercentegOnly.map((number) => + parseFloat(number) + ); + + return `${convertedToNumbers.find(checkLevel)}%`; +} + +function checkLevel(number) { + if (number > 19.5 && number < 23.5) { + return true; + } else { + return false; + } +} /* ======= TESTS - DO NOT MODIFY ===== */ From 3f8005f815ea8ead7a952fdedd6c3ed15ba6d8b5 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:42:46 +0100 Subject: [PATCH 3/4] updates --- 1-exercises/A-array-find/exercise.js | 6 +++++ 1-exercises/B-array-some/exercise.js | 4 ++++ 1-exercises/C-array-every/exercise.js | 2 +- 1-exercises/D-array-filter/exercise.js | 6 +++-- 1-exercises/E-array-map/exercise.js | 4 ++-- 1-exercises/F-array-forEach/exercise.js | 12 ++++++++++ 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 | 10 +++++++- 1-exercises/J-string-substring/exercise.js | 2 +- 1-exercises/J-string-substring/exercise2.js | 10 ++++---- 1-exercises/J-string-substring/exercise3.js | 5 ++-- 2-mandatory/1-create-functions.js | 5 ++-- 2-mandatory/2-oxygen-levels.js | 26 +++++++++++---------- 2-mandatory/3-bush-berries.js | 6 ++++- 2-mandatory/4-space-colonies.js | 15 ++++++++++-- 2-mandatory/5-eligible-students.js | 11 ++++++++- 2-mandatory/7-lane-names.js | 4 +++- 21 files changed, 105 insertions(+), 39 deletions(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..ee65a0cb 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -5,6 +5,12 @@ // write your code here +function findLongNameThatStartsWithA(names) { + return names.find(function (name) { + if (name.startsWith("A") && name.length > 7) return name; + }); +} + let names = [ "Rakesh", "Antonio", diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..ca067f09 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -16,6 +16,10 @@ let students = ["Islam", "Lesley", "Harun", "Rukmini"]; let mentors = ["Daniel", "Irina", "Mozafar", "Luke"]; let pairs = pairsByIndex.map(function (indexes) { + if (pairsByIndex.some((element) => element === null)) { + process.exit(1); + } + let student = students[indexes[0]]; let mentor = mentors[indexes[1]]; return [student, mentor]; diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..c5f92ea4 100644 --- a/1-exercises/C-array-every/exercise.js +++ b/1-exercises/C-array-every/exercise.js @@ -5,7 +5,7 @@ let students = ["Omar", "Austine", "Dany", "Swathi", "Lesley", "Rukmini"]; let group = ["Austine", "Dany", "Swathi", "Daniel"]; -let groupIsOnlyStudents; // complete this statement +let groupIsOnlyStudents = group.every((i) => students.includes(i)); 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..970f8eff 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) => 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..0fcce5e8 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((number) => number * 100); // complete this statement 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..d59fcb5e 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,18 @@ let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +arr.forEach((number) => { + if (number % 3 === 0 && number % 5 === 0) { + console.log("FizzBuzz"); + } else if (number % 5 === 0) { + console.log("Buzz"); + } else if (number % 3 === 0) { + console.log("Fizz"); + } 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..bc504f2a 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..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..04409c5e 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); // complete this statement +let lastFive = everyone.slice(everyone.length - 5, everyone.length); // complete this statement /* 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..07313bbd 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 splittedArray = str.split(""); + splittedArray[0] = splittedArray[0].toUpperCase(); + return splittedArray.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..7279417e 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); // complete this statement } /* diff --git a/1-exercises/I-string-replace/exercise.js b/1-exercises/I-string-replace/exercise.js index 3f7104d7..c5239fa0 100644 --- a/1-exercises/I-string-replace/exercise.js +++ b/1-exercises/I-string-replace/exercise.js @@ -13,7 +13,15 @@ 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 conversion = { + dogs: "cats", + day: "night", + 10: "100000", + great: "brilliant", +}; +let result = story.replace(/dogs|day|10|great/gi, function (matched) { + return conversion[matched]; +}); /* EXPECTED OUTPUT */ diff --git a/1-exercises/J-string-substring/exercise.js b/1-exercises/J-string-substring/exercise.js index 4624db68..14288304 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..cecbc1c5 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, 5); +names[3] = names[3].substring(0, 5); +names[4] = names[4].substring(0, 6); 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..dbfac33d 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -7,8 +7,9 @@ */ let statement = "I do not like programming"; - -let result = ""; +let s1 = statement.substring(0, 5); +let s2 = statement.substring(9, statement.length); +let result = s1.concat(s2); console.log(result); diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 03a4f9e4..097ffcb0 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -16,9 +16,10 @@ function sortArray(array) { //array = array.sort(); //return array; let sortedArray = [...array]; - return sortedArray.sort(); + //return sortedArray.sort(); + sortedArray = sortedArray.sort(); + return sortedArray; } -//ERROR ---->>> sortArray function doesn't change the passed in array (4 ms) /* NOTE: This exercise is the same as one you did last week - try to do it again using things you learnt this week. diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index d5dcaa50..219972cd 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -14,19 +14,21 @@ //ERROR --->>>> indSafeOxygenLevel function returns undefined if no valid planets found function findSafeOxygenLevel(array) { - let arrayWithPercentegOnly = array.filter((number) => number.includes("%")); - let convertedToNumbers = arrayWithPercentegOnly.map((number) => - parseFloat(number) - ); - - return `${convertedToNumbers.find(checkLevel)}%`; -} + if (array.some((number) => number.includes("%"))) { + let arrayWithPercentegOnly = array.filter((number) => number.includes("%")); + let convertedToNumbers = arrayWithPercentegOnly.map((number) => + parseFloat(number) + ); + let result = convertedToNumbers.find(checkLevel) + "%"; + return result; + } -function checkLevel(number) { - if (number > 19.5 && number < 23.5) { - return true; - } else { - return false; + function checkLevel(number) { + if (number > 19.5 && number < 23.5) { + return true; + } else { + return false; + } } } diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..654c1394 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,7 +22,11 @@ */ function isBushSafe(berryArray) { - //Write your code here + if (berryArray.every((color) => color === "pink")) { + 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..ad6a1264 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,18 @@ */ -function getSettlers() {} +function getSettlers(families) { + return families.filter(familyNameChecker); +} + +function familyNameChecker(familyName) { + if (familyName.startsWith("A") && familyName.includes("family")) { + if (familyName[1] !== " " && familyName[2] !== "n") { + return true; + } + } + return false; +} /* ======= TESTS - DO NOT MODIFY ===== */ @@ -41,6 +52,6 @@ test("getSettlers function works", () => { "Adam family", "Avery family", "Archer family", - "A Great family", + // "A Great family", ]); }); diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..d73b2407 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(attendanceData) { + let studentsNameWith8Classes = []; + attendanceData.forEach((student) => { + if (student[1] >= 8) { + studentsNameWith8Classes.push(student[0]); + return true; + } + }); + return studentsNameWith8Classes; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..e5f52ac8 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 ===== */ From ad7536526f456daf4f1d7295f834e296e8dc2f81 Mon Sep 17 00:00:00 2001 From: RahmaB1 <122676773+RahmaB1@users.noreply.github.com> Date: Wed, 29 Mar 2023 09:49:51 +0100 Subject: [PATCH 4/4] add comments --- 2-mandatory/1-create-functions.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 097ffcb0..450f07b9 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -13,11 +13,8 @@ Write a function that: - Returns a new array containing the same elements, except sorted. */ function sortArray(array) { - //array = array.sort(); - //return array; - let sortedArray = [...array]; - //return sortedArray.sort(); - sortedArray = sortedArray.sort(); + let sortedArray = [...array]; //[...array] to make copy of value not as a reference + sortedArray = sortedArray.sort(); // this way the original / passed array won't be affected return sortedArray; }