Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

LND10 | Adrian Ilovan | JS1-Coursework-Week4 #241

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions 2-mandatory/1-create-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ 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);
}

/*
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();
}

/*
Expand All @@ -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());
}

/*
Expand All @@ -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));
}

/*
Expand All @@ -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 ===== */
Expand Down
9 changes: 8 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */

Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */
Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */

Expand Down
10 changes: 9 additions & 1 deletion 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <attendance.length; i++) {
if (attendance[i][1] >= 8) {
eligibleStudentNames.push(attendance[i][0]);
}
}
return eligibleStudentNames;
}

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
19 changes: 14 additions & 5 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 ===== */
Expand Down
5 changes: 4 additions & 1 deletion 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */

Expand Down
26 changes: 25 additions & 1 deletion 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down