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

London Class 10- Yuliya Hospodar- JavaScript-Core-1-Coursework-Week4 #248

Open
wants to merge 2 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
26 changes: 20 additions & 6 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(arr) {
return arr.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(arr ) {
return arr.slice( ).sort( )
}

/*
Expand All @@ -24,7 +26,10 @@ Write a function that:
- Removes any forward slashes (/) in the strings.
- Makes the strings all lowercase.
*/
function tidyUpString() {
function tidyUpString(arr) {
return arr.map(string =>{
return string.trim().replace(/\//g, '').toLowerCase()
})
}

/*
Expand All @@ -33,7 +38,10 @@ 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) {
let newArr = arr.slice()
newArr.splice(index, 1)
return newArr;
}

/*
Expand All @@ -44,7 +52,13 @@ Write a function that:
- Numbers greater 100 must be replaced with 100.
*/

function formatPercentage() {
function formatPercentage(arr) {

return arr.map(value =>{
let min = Math.min(value, 100);
let rounded = Math.round(100 * min) / 100;
return `${rounded}%`;
})
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -142,4 +156,4 @@ test("formatPercentage function works", () => {
"100%",
"0.37%",
]);
});
});
16 changes: 14 additions & 2 deletions 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}
function findSafeOxygenLevel(oxygenLevels) {
const safeLevel = oxygenLevels
.filter(index => {
return index.endsWith("%")
})
.find(index => {
index = parseFloat(index.replace("%", ""))
const lower = 19.5;
const highest = 23.5;
return lower < index && index < highest;
})
return safeLevel;
}

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

Expand All @@ -35,4 +47,4 @@ test("findSafeOxygenLevel function filters out invalid percentages", () => {

test("findSafeOxygenLevel function returns undefined if no valid planets found", () => {
expect(findSafeOxygenLevel(["50"])).toBeUndefined();
});
});
12 changes: 10 additions & 2 deletions 2-mandatory/3-bush-berries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@
*/

function isBushSafe(berryArray) {
//Write your code here
const safe = "Bush is safe to eat from";
const notSafe = "Toxic! Leave bush alone!";

const isSafe = berryArray.every((index) => {
return index === "pink";
});

return isSafe ? safe : notSafe;
}


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

test("isBushSafe finds toxic busy", () => {
Expand All @@ -37,4 +45,4 @@ test("isBushSafe function finds safe bush", () => {
expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual(
"Bush is safe to eat from"
);
});
});
19 changes: 12 additions & 7 deletions 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/*
The voyagers decide that they quite like this planet, and some of them want to settle there and colonise it.
The voyagers decide that they quite like this planet, and some of them want to settle
there and colonise it.

They call the planet "Alpha" and they decide that the FAMILIES whose last names start with 'A' should stay,
They call the planet "Alpha" and they decide that the FAMILIES whose last names start with
'A' should stay,
while the others go on in search of other planets to call home.

Create a function that returns an array of colonisers that will stay, according to the above rules.

NOTE: don't include any element that is not a "family".

HINT: Whenever you read the above the instructions, try to come up with the main input and output and logic
HINT: Whenever you read the above the instructions, try to come up with the main input
and output and logic
Input: Is an array
Output: Is an array
Logic: Only strings that start with A, and finish with family

*/

function getSettlers() {}

function getSettlers(family) {
return family.filter(i => {
return i.startsWith("A") && i.endsWith(" family")
})
}
/* ======= TESTS - DO NOT MODIFY ===== */

test("getSettlers function works", () => {
Expand All @@ -43,4 +48,4 @@ test("getSettlers function works", () => {
"Archer family",
"A Great family",
]);
});
});
14 changes: 9 additions & 5 deletions 2-mandatory/5-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
(see tests to confirm how this data will be structured)
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function getEligibleStudents() {}

function getEligibleStudents(students) {
const eligibleStduent = students.filter(index =>{
const attendanceCount = index[1]
return attendanceCount >= 8;
})
const names = eligibleStduent.map(index => index[0])
return names;
}
/* ======= TESTS - DO NOT MODIFY ===== */

test("getEligibleStudents function works", () => {
const attendance = [
["Ahmed", 8],
Expand All @@ -31,4 +35,4 @@ test("getEligibleStudents function works", () => {
test("getEligibleStudents function can return empty array", () => {
const attendance = [["Jacob", 7]];
expect(getEligibleStudents(attendance)).toEqual([]);
});
});
35 changes: 28 additions & 7 deletions 2-mandatory/6-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
function checkCodeIsThere(stringText) {
let magicWord = "code";
//edit code below
if (stringText) {
return stringText;
const containsCode = stringText.includes(magicWord);
if (containsCode) {
return stringText.indexOf(magicWord);
} else {
return "Not found";
}
}


/*
I am new to London and would like to know what transport I can take to different famous locations.
The input provided contains a list of locations in London. Each of locations is followed by a list
Expand Down Expand Up @@ -64,7 +66,11 @@ function checkCodeIsThere(stringText) {

Hint: Use the corresponding array method to split the array.
*/
function getTransportModes() {}

function getTransportModes(locationAndModes) {
return locationAndModes.slice(1);
}


/*
Implement the function isAccessibleByTransportMode that
Expand All @@ -81,7 +87,11 @@ 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 +102,10 @@ 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 @@ -121,8 +134,16 @@ function getLocationName() {}

Advanced challange: try to use arrow function when invoking an array method.
*/


function journeyPlanner(locations, transportMode) {
// Implement the function body
return locations
.filter((location) => {
return isAccessibleByTransportMode(location, transportMode);
})
.map((accessibleLocation) => {
return getLocationName(accessibleLocation);
});
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -209,4 +230,4 @@ describe("journeyPlanner", () => {
"Tower Bridge",
]);
});
});
});
7 changes: 4 additions & 3 deletions 2-mandatory/7-lane-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

HINT: string and array methods that could be helpful (indexOf, filter)
*/

function getLanes() {}
function getLanes(streetNames) {
return streetNames.filter(i => i.indexOf('Lane') !== -1);
}

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

Expand All @@ -20,4 +21,4 @@ test("getLanes function works", () => {
];

expect(getLanes(streetNames)).toEqual(["Abchurch Lane", "Addle Lane"]);
});
});
41 changes: 38 additions & 3 deletions 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,43 @@ Expected Result:
PasswordValidationResult= [false, false, false, false, true]

*/

function validatePasswords(passwords) {}
function validatePasswords(passwords) {
const passwordSet = new Set();
return passwords.map(password => {
if (password.length < 5) {
return false;
}
let hasUppercaseLetter = false;
let hasLowercaseLetter = false;
let hasNumber = false;
let hasSymbol = false;
for (let i = 0; i < password.length; i++) {
const char = password.charAt(i);
switch (true) {
case /[A-Z]/.test(char):
hasUppercaseLetter = true;
break;
case /[a-z]/.test(char):
hasLowercaseLetter = true;
break;
case /[0-9]/.test(char):
hasNumber = true;
break;
case /[!#$%.*&]/.test(char):
hasSymbol = true;
break;
}
}
if (!hasUppercaseLetter || !hasLowercaseLetter || !hasNumber || !hasSymbol) {
return false;
}
if (passwordSet.has(password)) {
return false;
}
passwordSet.add(password);
return true;
});
}

// Returns true if string contains at least one uppercase letter.
function containsUppercaseLetter(string) {
Expand Down Expand Up @@ -69,4 +104,4 @@ test("Example 2", () => {
"Pl3nty!",
])
).toEqual([true, true, false, false, false]);
});
});