diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..66eda423 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(inputArray) { + firstFiveNumbersArr = inputArray.slice(0, 5); + return firstFiveNumbersArr; } /* @@ -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(inputArray) { + let sortedArr = inputArray.sort(); + return sortedArr; } /* @@ -24,7 +28,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 +51,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; } /* @@ -43,8 +69,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 ===== */ 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 ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..d64d3609 100644 --- a/2-mandatory/3-bush-berries.js +++ b/2-mandatory/3-bush-berries.js @@ -16,13 +16,21 @@ 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. */ +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 ===== */ 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 ===== */ 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 ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..758af7a4 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" @@ -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"; } @@ -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 @@ -81,7 +84,12 @@ function getTransportModes() {} 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 @@ -92,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. @@ -122,7 +132,10 @@ function getLocationName() {} 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 ===== */