From a5335fbee21ec320cf4480871656e1c242f11e85 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 12:31:07 +0000 Subject: [PATCH 01/20] Update exercise.js --- 1-exercises/A-array-find/exercise.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35902fed..35e461c2 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -16,10 +16,16 @@ let names = [ "Karim", "Ahmed", ]; - +/* let longNameThatStartsWithA = findLongNameThatStartsWithA(names); console.log(longNameThatStartsWithA); +const found = names.find(element => element[0] === "A" );*/ + + + +const found = names.find(element => element[0] === "A" && element.length ); + /* EXPECTED OUTPUT */ // "Alexandra" From 7818e2d074242ed0ed63c8f60572fc4f2515a43d Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 12:41:03 +0000 Subject: [PATCH 02/20] Update exercise.js --- 1-exercises/A-array-find/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-exercises/A-array-find/exercise.js b/1-exercises/A-array-find/exercise.js index 35e461c2..998e16a2 100644 --- a/1-exercises/A-array-find/exercise.js +++ b/1-exercises/A-array-find/exercise.js @@ -25,7 +25,7 @@ const found = names.find(element => element[0] === "A" );*/ -const found = names.find(element => element[0] === "A" && element.length ); +const found = names.find(element => element[0] === "A" && element.length > 7 ); /* EXPECTED OUTPUT */ // "Alexandra" From 29781629da84ba63e416020083dcad4c345f8d5e Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 12:42:34 +0000 Subject: [PATCH 03/20] Update exercise.js --- 1-exercises/B-array-some/exercise.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/1-exercises/B-array-some/exercise.js b/1-exercises/B-array-some/exercise.js index fddc69ee..cb64524e 100644 --- a/1-exercises/B-array-some/exercise.js +++ b/1-exercises/B-array-some/exercise.js @@ -6,6 +6,12 @@ - Do not edit any of the existing code */ +for (const pair of pairsByIndex) { + if (!pair) { + process.exit(1); + } +} + let pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]]; // If there is a null value in the array exit the program with the error code From 90d6bcbd03108f3bc1c39052ca5db632eb40a2bc Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 12:59:31 +0000 Subject: [PATCH 04/20] slice method used --- 1-exercises/H-array-methods-2/exercise.js | 4 ++-- 1-exercises/J-string-substring/exercise.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/1-exercises/H-array-methods-2/exercise.js b/1-exercises/H-array-methods-2/exercise.js index 59c5daa7..45fd2c5e 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(-5); // complete this statement /* DO NOT EDIT BELOW THIS LINE 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); From a2565f73bcce877b3f232e5a49ae8acf429d2f8e Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 13:41:21 +0000 Subject: [PATCH 05/20] Update exercise2.js --- 1-exercises/H-array-methods-2/exercise2.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/1-exercises/H-array-methods-2/exercise2.js b/1-exercises/H-array-methods-2/exercise2.js index 14bb4318..99b8823e 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 strString = str.split("") + let strFirst = strString[0].toUpperCase() + let strCombined = strFirst.concat(str.slice(1)) + return strCombined + +} /* DO NOT EDIT BELOW THIS LINE From 5b52d39d966e0ce1ccd31e7947ecb7574e45094f Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 14:15:03 +0000 Subject: [PATCH 06/20] Update exercise3.js --- 1-exercises/H-array-methods-2/exercise3.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/1-exercises/H-array-methods-2/exercise3.js b/1-exercises/H-array-methods-2/exercise3.js index c8e079e4..6a832239 100644 --- a/1-exercises/H-array-methods-2/exercise3.js +++ b/1-exercises/H-array-methods-2/exercise3.js @@ -7,7 +7,12 @@ let ukNations = ["Scotland", "Wales", "England", "Northern Ireland"]; function isInUK(country) { - return; // complete this statement +if (ukNations.includes(country)){ +return true +} + else{ + return "Not a UK country" + } } /* From c35ae89f07cfd52f7c3f26f10f12299ed31011d8 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Fri, 17 Mar 2023 20:53:50 +0000 Subject: [PATCH 07/20] Update 1-create-functions.js --- 2-mandatory/1-create-functions.js | 55 ++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..60d9981a 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) { + for (let i =0; i < 5 ; i++ ){ + return arr[i] } /* @@ -11,7 +13,9 @@ 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) { + let arrSorted = arr.sort() + return arrSorted } /* @@ -24,7 +28,11 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { +function tidyUpString(str) { + trimStr = str.trim(); + mystring = trimStr.replace('/',''); + lowerStr = mystring.toLowerCase() + } /* @@ -33,7 +41,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(arr, index) { + arr.splice(index, 1); + return arr; } /* @@ -44,7 +54,42 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +/*function formatPercentage(arr) { +let arrD = [] +let arrE = [] +let arrB = arr.toString() +console.log(arrB) +let arrC = arrB.split(",") +console.log(arrC) +for (let i = 0; i < arrC.length; i++){ + arrD.push(arrC[i] + "!") +} + +console.log(arrD) + +for (let num of arrD){ +if (num.length > 3){ +arrE.push('100%') +} +else{ +arrE.push(num) +} + + + } + +formatPercentage([1,2,1000,5,6,34])*/ + +function formatPercentage(arr) { + let result = []; + for (const number of arr) { + if (number > 100) { + result.push('100%') + } else { + result.push(`${Number(number.toFixed(2))}%`) + } + } + return result; } /* ======= TESTS - DO NOT MODIFY ===== */ From aa43df1b7c56e0e24adc882adeafd0147c1ac0cb Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 18 Mar 2023 09:50:46 +0000 Subject: [PATCH 08/20] Update 3-bush-berries.js --- 2-mandatory/3-bush-berries.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..086b443e 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,6 +22,11 @@ */ function isBushSafe(berryArray) { + if (berryArray.every() == 'pink') + {return "Bush is safe to eat"} + else{ + return "Toxic! Leave bush alone!" + } //Write your code here } From ef92ea049c41fe56c2280152a9e37599b83a974c Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 18 Mar 2023 10:02:54 +0000 Subject: [PATCH 09/20] updates to JS1 week4 -Use of ternary operator -if statements in for loops --- 2-mandatory/2-oxygen-levels.js | 16 +++++++++++++++- 2-mandatory/3-bush-berries.js | 9 +++++++++ 2-mandatory/4-space-colonies.js | 4 +++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..1210312c 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,21 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel() { + 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 ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index 086b443e..eef90194 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -30,6 +30,15 @@ function isBushSafe(berryArray) { //Write your code here } +/* + const bushIsSafe = berryArray.every((color) => color == "pink"); + const output = bushIsSafe ? "Bush is safe to eat from" : "Toxic! Leave bush alone!"; + return output; + + + +*/ + /* ======= TESTS - DO NOT MODIFY ===== */ test("isBushSafe finds toxic busy", () => { diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..128b7dae 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,9 @@ */ -function getSettlers() {} +function getSettlers() { + return allpeople.filter(person => person.includes('family') && person.startsWith('A')); +} /* ======= TESTS - DO NOT MODIFY ===== */ From 321d220d4320343ddd11530ef5eac89ec62ff051 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 18 Mar 2023 13:54:44 +0000 Subject: [PATCH 10/20] Update 5-eligible-students.js --- 2-mandatory/5-eligible-students.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..0d5825e8 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,12 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents() { + let examClass = []; + for (student in attendance){ + if student[1] + } +} /* ======= TESTS - DO NOT MODIFY ===== */ From ff540dec2f5e6146ebc313c3a0a19f78f751f489 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 18 Mar 2023 14:26:41 +0000 Subject: [PATCH 11/20] Update 6-journey-planner.js --- 2-mandatory/6-journey-planner.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..4b13d761 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -20,7 +20,7 @@ function checkCodeIsThere(stringText) { let magicWord = "code"; //edit code below - if (stringText) { + if (stringText.includes("code")) { return stringText; } 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(arr) { + return arr.splice(1) +} /* Implement the function isAccessibleByTransportMode that @@ -81,7 +83,12 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(arr, transport) { +if (arr.includes(transport)){ +console.log(true) +}else{console.log(false)} +} + /* Implement the function getLocationName that @@ -92,7 +99,9 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(arr) { + return arr[0] +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. From 3dd9ef1858f033dae29488b4101cfd15cc3efdf5 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 08:09:07 +0000 Subject: [PATCH 12/20] Update 3-bush-berries.js --- 2-mandatory/3-bush-berries.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index eef90194..aa4b2068 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -22,22 +22,19 @@ */ function isBushSafe(berryArray) { - if (berryArray.every() == 'pink') - {return "Bush is safe to eat"} - else{ - return "Toxic! Leave bush alone!" + if (berryArray.every() == "pink") { + return "Bush is safe to eat"; + } else { + return "Toxic! Leave bush alone!"; } //Write your code here } -/* - const bushIsSafe = berryArray.every((color) => color == "pink"); - const output = bushIsSafe ? "Bush is safe to eat from" : "Toxic! Leave bush alone!"; - return output; - - - -*/ +const bushIsSafe = berryArray.every((color) => color == "pink"); +const output = bushIsSafe + ? "Bush is safe to eat from" + : "Toxic! Leave bush alone!"; +return output; /* ======= TESTS - DO NOT MODIFY ===== */ From 56e199f92e865a1c5cc21c6c39b4569566da4995 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 08:30:19 +0000 Subject: [PATCH 13/20] exercise C --- 1-exercises/C-array-every/exercise.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/1-exercises/C-array-every/exercise.js b/1-exercises/C-array-every/exercise.js index 347b9632..f36c336f 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 = function () { + for (let i = 0; i < students.length; i++) { + if (!group.includes(student[i])) { + return true; + } + } +}; // complete this statement if (groupIsOnlyStudents) { console.log("The group contains only students"); From a026110c8e37c514a8fad8fe0d4d86ff8b06cdb2 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 09:42:22 +0000 Subject: [PATCH 14/20] Filter - with a function Exercise D filter method used to change array according to conditions set in a function --- 1-exercises/D-array-filter/exercise.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/1-exercises/D-array-filter/exercise.js b/1-exercises/D-array-filter/exercise.js index 51837028..bd613343 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(function (item) { + return Array.isArray(item) && item.length === 2; +}); // Complete this statement 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 +*/ From ddee7c204e19330d8991df050f1a3eaa49368249 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 10:02:44 +0000 Subject: [PATCH 15/20] Exercise E - map using map to multiply elements in an array --- 1-exercises/E-array-map/exercise.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-exercises/E-array-map/exercise.js b/1-exercises/E-array-map/exercise.js index 5a157279..dd28253a 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((element) => element * 100); // complete this statement console.log(numbersMultipliedByOneHundred); /* EXPECTED RESULT [10, 20, 30, 40, 50] -*/ \ No newline at end of file +*/ From c17ee4c1ad3ffece0b9b71ef4f3654e386fe5560 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 10:26:31 +0000 Subject: [PATCH 16/20] FizzBuzz problem with forEach() --- 1-exercises/F-array-forEach/exercise.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/1-exercises/F-array-forEach/exercise.js b/1-exercises/F-array-forEach/exercise.js index 985068cc..6fb63357 100644 --- a/1-exercises/F-array-forEach/exercise.js +++ b/1-exercises/F-array-forEach/exercise.js @@ -9,6 +9,19 @@ 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 % 3 === 0) { + console.log("Fizz"); + } else if (number % 5 === 0) { + console.log("Buzz"); + } else { + console.log(number); + } +}); + +console.log(filteredNumbers); /* EXPECTED OUTPUT */ /* From dced800626f2fb49fb6aac11e1a4e1c584a5b563 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 10:48:27 +0000 Subject: [PATCH 17/20] Update exercise.js concat method used on an array with a spread operator --- 1-exercises/G-array-methods/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From f9a6f1ecad02dacd47bda07d8291a8927bd6ed8b Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 10:48:37 +0000 Subject: [PATCH 18/20] Update exercise2.js spread operator --- 1-exercises/J-string-substring/exercise2.js | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/1-exercises/J-string-substring/exercise2.js b/1-exercises/J-string-substring/exercise2.js index a1d9bf62..8a70eace 100644 --- a/1-exercises/J-string-substring/exercise2.js +++ b/1-exercises/J-string-substring/exercise2.js @@ -24,6 +24,28 @@ names.forEach((name) => { console.log(name); }); +/*Other function j + +function getFirstNames (){ +let splitNames =[] +for(let i =0; i Date: Tue, 21 Mar 2023 10:49:01 +0000 Subject: [PATCH 19/20] Update exercise2.js concat an array using ... --- 1-exercises/G-array-methods/exercise2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-exercises/G-array-methods/exercise2.js b/1-exercises/G-array-methods/exercise2.js index 4c68c3a6..d49597b6 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 = mentor.concat(...students); // complete this statement /* DO NOT EDIT BELOW THIS LINE From 6ef8eae1d4735d5b721d41a1dc6ea0fbfdda7b35 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Tue, 21 Mar 2023 11:25:43 +0000 Subject: [PATCH 20/20] J- exercise 3 Use of substring() to remove words from an array. --- 1-exercises/J-string-substring/exercise3.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/1-exercises/J-string-substring/exercise3.js b/1-exercises/J-string-substring/exercise3.js index 14f77417..a8e42667 100644 --- a/1-exercises/J-string-substring/exercise3.js +++ b/1-exercises/J-string-substring/exercise3.js @@ -7,11 +7,22 @@ */ let statement = "I do not like programming"; - let result = ""; -console.log(result); +let wordToRemove = "not"; + +let index = statement.indexOf(wordToRemove); + +if (index !== -1) { + let length = wordToRemove.length; + + let firstPart = statement.substring(0, index); + let secondPart = statement.substring(index + length); + + result = firstPart + secondPart; + console.log(result); +} /* EXPECTED OUTPUT "I do like programming"