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

London10-Hussein-Bahdon-JS1-Week 4 #242

Open
wants to merge 1 commit 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
24 changes: 19 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(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
23 changes: 22 additions & 1 deletion 2-mandatory/2-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,28 @@
Some string methods that might help you here are .replace() and .substring().
*/

function findSafeOxygenLevel() {}




function findSafeOxygenLevel(oxygenLevels) {

const safeLevels = 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 safeLevels;



}

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

Expand Down
10 changes: 9 additions & 1 deletion 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 safeToEat = "Bush is safe to eat from";
const notSafeEat = "Toxic! Leave bush alone!";

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

return isSafeToEat ? safeToEat : notSafeEat;
}


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

test("isBushSafe finds toxic busy", () => {
Expand Down
15 changes: 11 additions & 4 deletions 2-mandatory/4-space-colonies.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
/*
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 ===== */

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(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 ===== */

Expand Down
33 changes: 27 additions & 6 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
5 changes: 3 additions & 2 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 Down
38 changes: 37 additions & 1 deletion 2-mandatory/8-password-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,43 @@ 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