From b377559554d189bb70f2d0abdd09e50ce91785eb Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Fri, 17 Mar 2023 23:10:58 +0000 Subject: [PATCH 01/14] started 1-create-functions tasks created the functions for first5(), sortArray(), tidyUpString() and remove(). --- 2-mandatory/1-create-functions.js | 36 +++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..68f72b5e 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -3,7 +3,12 @@ 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(inputArray) { + firstFiveNumbersArr = []; + for (let i = 0; i < 5; i++) { + firstFiveNumbersArr.push(inputArray[i]); + } + return firstFiveNumbersArr; } /* @@ -11,7 +16,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(inputArray) { + return inputArray.sort(); } /* @@ -24,7 +30,21 @@ Write a function that: - Removes any forward slashes (/) in the strings. - Makes the strings all lowercase. */ -function tidyUpString() { + +function removeSpaces(string) { + return string.trim(); +} + +function removeSlashes(string) { + return string.replace("/", "") +} + +function makeLowercase(string) { + return string.toLowerCase(); +} + +function tidyUpString(inputArray) { + return inputArray.map(makeLowercase).map(removeSlashes).map(removeSpaces); } /* @@ -33,7 +53,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(inputArray, index) { + let returnArr = []; + for (let i = 0; i < index; i++) { + returnArr.push(inputArray[i]) + } + for (let i = index + 1; i < inputArray.length; i++) { + returnArr.push(inputArray[i]); + } + return returnArr; } /* From 514d90fa3c6593315d174e53c7358740597440a6 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 00:14:35 +0000 Subject: [PATCH 02/14] created format percentage function continued with exercise 1-create-functions. --- 2-mandatory/1-create-functions.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 68f72b5e..98862836 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -36,7 +36,7 @@ function removeSpaces(string) { } function removeSlashes(string) { - return string.replace("/", "") + return string.replace("/", ""); } function makeLowercase(string) { @@ -56,7 +56,7 @@ Write a function that: function remove(inputArray, index) { let returnArr = []; for (let i = 0; i < index; i++) { - returnArr.push(inputArray[i]) + returnArr.push(inputArray[i]); } for (let i = index + 1; i < inputArray.length; i++) { returnArr.push(inputArray[i]); @@ -71,8 +71,24 @@ Write a function that: - The numbers must be rounded to 2 decimal places. - Numbers greater 100 must be replaced with 100. */ +function convertToPercentage(number) { + return `${number}%`; +} + +function roundNumber(number) { + return Math.round(number * 100) / 100; +} + +function belowhundred(number) { + if (number <= 100) { + return number; + } else { + return 100; + } +} -function formatPercentage() { +function formatPercentage(inputArray) { + return inputArray.map(belowhundred).map(roundNumber).map(convertToPercentage); } /* ======= TESTS - DO NOT MODIFY ===== */ From 4498a80b8eb4203359d449c92c1c4265556a519e Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 07:17:21 +0000 Subject: [PATCH 03/14] refactored first5 function - npm test failed as loop was passing undefined when array has less than 5 items. - could use a check on array length before passing loop but slice method looks like better option (less code). - changed function to use slice method to replace loop. --- 2-mandatory/1-create-functions.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 98862836..66eda423 100644 --- a/2-mandatory/1-create-functions.js +++ b/2-mandatory/1-create-functions.js @@ -4,10 +4,7 @@ Write a function that: - Returns a new array containing the first five elements of the passed array. */ function first5(inputArray) { - firstFiveNumbersArr = []; - for (let i = 0; i < 5; i++) { - firstFiveNumbersArr.push(inputArray[i]); - } + firstFiveNumbersArr = inputArray.slice(0, 5); return firstFiveNumbersArr; } @@ -17,7 +14,8 @@ Write a function that: - Returns a new array containing the same elements, except sorted. */ function sortArray(inputArray) { - return inputArray.sort(); + let sortedArr = inputArray.sort(); + return sortedArr; } /* From d76f810c21474aaea588cdb6580aa58b9e94246a Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 07:23:24 +0000 Subject: [PATCH 04/14] attempted oxygen-levels function - created a function to remove % symbol from number string. - created a function to remove values less than 19.5 - created a function to remove values greater than 23.5 - used the functions created to pass input string through and returned the first value using index 0 of the new array --- 2-mandatory/2-oxygen-levels.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..7f72f1fc 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,25 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function removePercentage(percentString) { + percentString.replace("%", ""); +} + +function isAboveMinOxygenLevel(numberString) { + return 19.5 > Number(numberString); +} + +function isBelowMaxOxygenLevel(numberString) { + return Number(numberString) < 23.5; +} + +function findSafeOxygenLevel(input) { + validLevelsArr = input + .map(removePercentage) + .filter(isAboveMinOxygenLevel) + .filter(isBelowMaxOxygenLevel); + return validLevelsArr[0]; +} /* ======= TESTS - DO NOT MODIFY ===== */ From 17bfbaf5b722908840b7b68ec59831f8dbfb154b Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 07:28:12 +0000 Subject: [PATCH 05/14] fixed typo watch -> what --- 2-mandatory/3-bush-berries.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..abeebba0 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -16,7 +16,7 @@ The .some() method tests to see if some of the values (at least 1) in an array match what you're looking for and returns true or false. - The .every() method will only return true if all values match watch you're looking for. + The .every() method will only return true if all values match what you're looking for. Let's first look at an example that will teach you how to use these methods. */ From c47ab25ee47c9b17a99e2a0a7f72f0a43baa9816 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 07:53:33 +0000 Subject: [PATCH 06/14] created bush berries function - created a isPink function to check if colour of berry equals pink - passed the input string through isPink function using .every() method which should return a string based on the boolean value of .every() --- 2-mandatory/3-bush-berries.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index abeebba0..d64d3609 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -21,8 +21,16 @@ Let's first look at an example that will teach you how to use these methods. */ +function isPink(colour) { + return colour === "pink"; +} + function isBushSafe(berryArray) { - //Write your code here + if (berryArray.every(isPink) === true) { + return "Bush is safe to eat from"; + } else { + return "Toxic! Leave bush alone!"; + } } /* ======= TESTS - DO NOT MODIFY ===== */ From 5eddcda2c18497a8caa3e6b7df7c33060b0c688a Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 08:11:48 +0000 Subject: [PATCH 07/14] created get settlers function - created a function to check if string contains "family" - created a function to check if string begins with "A" - completed the get settlers function by using the .filter() method to create a new array with only the name of families that are staying on planet alpha --- 2-mandatory/4-space-colonies.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/2-mandatory/4-space-colonies.js b/2-mandatory/4-space-colonies.js index 30095213..1b000828 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,18 @@ */ -function getSettlers() {} +function isFamily(element) { + return element.includes("family"); +} + +function isNameStartwithA(name) { + return name[0] === "A"; +} + +function getSettlers(listOfElements) { + let validFamilies = listOfElements.filter(isFamily).filter(isNameStartwithA); + return validFamilies; +} /* ======= TESTS - DO NOT MODIFY ===== */ From cb66b7a269993d77dcbc42a31f3e054f78845f06 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 09:24:14 +0000 Subject: [PATCH 08/14] created get eligible students function - created a function to check if attendance is over 8 - created a function to return only first value when an array is passed - used .filter() method on input list of students to only return students with attendance greater than 8 - used .map() method to return only name --- 2-mandatory/5-eligible-students.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..cc255684 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,17 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function isAttendanceOver8(studentRecord) { + return studentRecord[1] >= 8; +} + +function returnName(studentRecord) { + return student[0]; +} + +function getEligibleStudents(studentList) { + return studentList.filter(isAttendanceOver8).map(returnName); +} /* ======= TESTS - DO NOT MODIFY ===== */ From 27223bd90b6a0cf2c97ca82280a7b48f6970e925 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 09:26:07 +0000 Subject: [PATCH 09/14] fixed typo paramter -> parameter --- 2-mandatory/6-journey-planner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..b3db5697 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -10,7 +10,7 @@ Now let's do this small exercise Using string methods update the checkCodeIsThere() function - - The function will have a string as a paramter + - The function will have a string as a parameter - The function should check if the word "code" exists in the string - If it does exist, return the index of it, if not return "Not found" From d4845545b5abdc72890d3c13caa9a9dbec3a4e3e Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 20:59:11 +0000 Subject: [PATCH 10/14] completed check code is there function - used .index() method to check if input string contains "code" word. - used .IndexOf() method to get the index of where "code" is situated in the string --- 2-mandatory/6-journey-planner.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index b3db5697..7b505d80 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"; } From e96c7a2f292efc30a30db8533f77d71ff74fca0a Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 21:09:56 +0000 Subject: [PATCH 11/14] completed get transport mode function - used .splice() method to return array without first item --- 2-mandatory/6-journey-planner.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 7b505d80..1b3544a5 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -64,7 +64,10 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} +function getTransportModes(locationTransportArr) { + let lastIndex = locationTransportArr.length; + return locationTransportArr.splice(1, lastIndex); +} /* Implement the function isAccessibleByTransportMode that From d354e2547cf7a1533d7136cc66fb71ab1dd04152 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 21:16:02 +0000 Subject: [PATCH 12/14] completed is accessible by transport mode function - used .includes() method to return a boolean value for if transport from (input2) is in array (input1) --- 2-mandatory/6-journey-planner.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 1b3544a5..d4be3b87 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -84,7 +84,12 @@ function getTransportModes(locationTransportArr) { Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode( + availableModeOfTransportArr, + chosenModeOfTransport +) { + return availableModeOfTransportArr.includes(chosenModeOfTransport); +} /* Implement the function getLocationName that From 89d3fbd7e44628c5517a5629cf4935c3891d20d0 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Sat, 18 Mar 2023 22:01:31 +0000 Subject: [PATCH 13/14] completed get location name function - used index() method with value 0 to access and return the first item of the input array --- 2-mandatory/6-journey-planner.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index d4be3b87..eb46650a 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -100,7 +100,9 @@ function isAccessibleByTransportMode( - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(locationTransportArr) { + return locationTransportArr[0]; +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. From 1401f79666d5bd5ba7c8d67039cbeaa3ed0fb700 Mon Sep 17 00:00:00 2001 From: ManSangSin <109699416+ManSangSin@users.noreply.github.com> Date: Mon, 20 Mar 2023 07:40:31 +0000 Subject: [PATCH 14/14] completed journey planner function - used to map() and filter() methods with previous functions to return an array with locations available when a transport type is specified. - only the name of the location is returned --- 2-mandatory/6-journey-planner.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index eb46650a..758af7a4 100644 --- a/2-mandatory/6-journey-planner.js +++ b/2-mandatory/6-journey-planner.js @@ -132,7 +132,10 @@ function getLocationName(locationTransportArr) { Advanced challange: try to use arrow function when invoking an array method. */ function journeyPlanner(locations, transportMode) { - // Implement the function body + let accessibleLocations = locations + .filter((location) => location.includes(transportMode)) + .map(getLocationName); + return accessibleLocations; } /* ======= TESTS - DO NOT MODIFY ===== */