diff --git a/2-mandatory/1-create-functions.js b/2-mandatory/1-create-functions.js index 6df12961..3b30980f 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,7 +12,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(array) { + return array.slice().sort(); } /* @@ -24,7 +26,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(str => str.trim().replace(/\//g, '').toLowerCase()); } /* @@ -33,7 +36,8 @@ 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) { + return array.slice(0, index).concat(array.slice(index + 1)); } /* @@ -44,7 +48,13 @@ Write a function that: - Numbers greater 100 must be replaced with 100. */ -function formatPercentage() { +function formatPercentage(array) { + return array.map(num => { + if (num > 100) { + num = 100; + } + return `${(Math.round(num * 100) / 100).toFixed(2)}%`; + }); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-oxygen-levels.js b/2-mandatory/2-oxygen-levels.js index 5711c5e5..703514fe 100644 --- a/2-mandatory/2-oxygen-levels.js +++ b/2-mandatory/2-oxygen-levels.js @@ -11,7 +11,14 @@ Some string methods that might help you here are .replace() and .substring(). */ -function findSafeOxygenLevel() {} +function findSafeOxygenLevel(planets) { + for (let i = 0; i < planets.length; i++) { + let oxygenLevel = parseFloat(planets[i].replace('%', '')); + if (!isNaN(oxygenLevel) && oxygenLevel >= 19.5 && oxygenLevel <= 23.5) { + return planets[i]; + } + } +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-bush-berries.js b/2-mandatory/3-bush-berries.js index b434a507..1fe3a22f 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((berry) => berry === "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..a8e2c52c 100644 --- a/2-mandatory/4-space-colonies.js +++ b/2-mandatory/4-space-colonies.js @@ -15,7 +15,11 @@ */ -function getSettlers() {} +function getSettlers(settledFamily) { + //We will filter the array to only include strings that start with "A" and end with "family" + const settlers = settledFamily.filter(str => str.startsWith('A') && str.endsWith('family')); + return settlers; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/5-eligible-students.js b/2-mandatory/5-eligible-students.js index f92478e0..eb422c91 100644 --- a/2-mandatory/5-eligible-students.js +++ b/2-mandatory/5-eligible-students.js @@ -7,7 +7,15 @@ - Returns an array containing only the names of the who have attended AT LEAST 8 classes */ -function getEligibleStudents() {} +function getEligibleStudents(attendance) { + const eligibleStudentNames = []; + for (let i = 0; i = 8) { + eligibleStudentNames.push(attendance[i][0]); + } + } + return eligibleStudentNames; +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/6-journey-planner.js b/2-mandatory/6-journey-planner.js index 25a14083..0588bc21 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"; } @@ -64,7 +64,9 @@ function checkCodeIsThere(stringText) { Hint: Use the corresponding array method to split the array. */ -function getTransportModes() {} +function getTransportModes(locationArray) { + return locationArray.slice(1); +} /* Implement the function isAccessibleByTransportMode that @@ -81,7 +83,9 @@ function getTransportModes() {} Hint: Use the corresponding array method to decide if an element is included in an array. */ -function isAccessibleByTransportMode() {} +function isAccessibleByTransportMode(transportModes, mode) { + return transportModes.includes(mode); +} /* Implement the function getLocationName that @@ -92,7 +96,9 @@ function isAccessibleByTransportMode() {} - Returns the name of the location e.g: "Tower Bridge" */ -function getLocationName() {} +function getLocationName(locationArray) { + return locationArray[0]; +} /* We arrived at the final method. it won't take long if you use the previously implemented functions wisely. @@ -123,6 +129,9 @@ function getLocationName() {} */ function journeyPlanner(locations, transportMode) { // Implement the function body + return locations + .filter(location => isAccessibleByTransportMode(getTransportModes(location), transportMode)) + .map(location => getLocationName(location)); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/7-lane-names.js b/2-mandatory/7-lane-names.js index 40c31b68..f4fa9050 100644 --- a/2-mandatory/7-lane-names.js +++ b/2-mandatory/7-lane-names.js @@ -6,7 +6,10 @@ HINT: string and array methods that could be helpful (indexOf, filter) */ -function getLanes() {} +function getLanes(streetNames) { + return streetNames.filter(name => name.includes("Lane")); + +} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/8-password-validator.js b/2-mandatory/8-password-validator.js index dc3e84d6..9c332d22 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) { + const seenPasswords = new Set(); + 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 (seenPasswords.has(password)) { + return false; + } + seenPasswords.add(password); + return true; + }); +} // Returns true if string contains at least one uppercase letter. function containsUppercaseLetter(string) {